duankeng1911 2016-07-28 20:54
浏览 27

通过PHP发送的MySQL查询没有得到好的结果

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探