I'm encountening some weird behavior from my php code. When I try to run this code :
<?php
function stmt_bind_assoc (&$stmt, &$out) {
$data = mysqli_stmt_result_metadata($stmt);
$fields = array();
$out = array();
$fields[0] = $stmt;
$count = 1;
while($field = mysqli_fetch_field($data)) {
$fields[$count] = &$out[$field->name];
$count++;
}
call_user_func_array(mysqli_stmt_bind_result, $fields);
}
//================================================
if (isset($_GET["chat_id"]) && !empty($_GET["chat_id"]) && isset($_GET["user_id"]) && !empty($_GET["user_id"]) ){
$servername = "localhost";
$username = "*****";
$password = "*****";
$db_name = "*****";
$id_chat=$_GET["chat_id"];
// Create connection
$db = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $db->prepare("SELECT `user_nicename`, `chat_msg`. * FROM `chat_msg`, `jzj5_users` WHERE `chat_msg`.`sender` = `jzj5_users`.`id` and `chat_msg`.`chat_id`=?");
$id_chat=intval($id_chat);
$stmt->bind_param('i', $id_chat);
$stmt->execute();
$stmt->store_result();
$resultrow = array();
stmt_bind_assoc($stmt, $resultrow);
$messages=array();
while($stmt->fetch())
{
$messages[]=$resultrow;
}
echo '<pre>'; print_r($messages); echo '</pre>';
echo json_encode($messages);
$stmt->close();
/*
$stmt = $conn->prepare("SELECT c . * , u.user_nicename FROM chat_msg AS c, jzj5_users AS u WHERE c.sender = u.id and c.chat_id=?")
*/
}
?>
With "?chat_id=1" for the get variables, I get that result :
Array (
[0] => Array
(
[id] => 3
[chat_id] => 1
[sender] => 2
[receiver] => 3
[msg] => Hello Julien!
[date_sent] => 2016-07-28 04:03:19
[read] => 0
[user_nicename] => popadmin
)
[1] => Array
(
[id] => 3
[chat_id] => 1
[sender] => 2
[receiver] => 3
[msg] => Hello Julien!
[date_sent] => 2016-07-28 04:03:19
[read] => 0
[user_nicename] => popadmin
)
[2] => Array
(
[id] => 3
[chat_id] => 1
[sender] => 2
[receiver] => 3
[msg] => Hello Julien!
[date_sent] => 2016-07-28 04:03:19
[read] => 0
[user_nicename] => popadmin
)
)
However, what I'm supposed to get is different. I tried to send the query using phpMyAdmin's control panel (after changing '?' into '1'), and I get the expected result.
I've been trying to fix that for an hour now, so if anyone has suggestion I'd be glad to read them.