SQL查询在PhpMyAdmin面板中运行但未在Php脚本中运行

时间:2016-10-17 11:48:24

标签: php mysql phpmyadmin

我有一个令我头疼的问题,当我输入phpmyadmin时它运行但是在运行放入脚本时没有产生任何输出甚至没有错误,我不能说它的连接或任何东西,我有另一个文件几乎与这一个完全一样,它完美无缺,但不是这一个,至少在这一点上甚至错误都会受到赞赏,下面是我的剧本:

<?php

require_once 'include/DB_Connect.php';
$db = new Db_Connect();
$conn = $db->connect();




// get all comments table
$result = $conn->prepare("SELECT sale_comments._id ,sale_comments._user_fk, 
                                 users._fname, users._lname, 
                                 sale_comments._comment,
                                 sale_comments._date_created 
                            FROM sale_comments, users 
                            WHERE sale_comments._user_fk = users._id 
                            ORDER BY sale_comments._date_created DESC ");
$result->execute();

$response["comments"] = array();
$result->bind_result($id, $user_fk, $fname, $lname, $comment, $date_created);


while($row = $result->fetch()) 
{
    $comment = array();
    $comment["id"] = $id;
    $comment["user_fk"] = $user_fk;
    $comment["fname"] =$fname;
    $comment["lname"] = $lname;  
    $comment["comment"] = $comment;
    $comment["date_created"] = $date_created;


    $response["message"] = "Loaded";
     $response["error"] = FALSE;
    array_push($response["comments"], $comment);

}

echo json_encode($response);

?>

我正在运行的另一个脚本

<?php


 require_once 'include/DB_Connect.php';
$db = new Db_Connect();
$conn = $db->connect();




// get all products from products table
 $result = $conn->prepare("SELECT _id, _user_fk, _title, _description, _price, _currency,  _category, _location, _image, _date_created FROM sales  ORDER BY _date_created DESC");
$result->execute();

$response["sales"] = array();
$result->bind_result($id, $user_fk, $title, $description, $price, $currency, $category, $location, $image, $date_created);


while($row = $result->fetch()) 
{
    $sale = array();
    $sale["id"] = $id;
    $sale["user_fk"] = $user_fk;  
    $sale["title"] = $title;
    $sale["description"] =$description;
    $sale["price"] = $price;
    $sale["currency"] = $currency;
    $sale["category"] = $category;
    $sale["location"] = $location;
    $sale["image"] = $image;
    $sale["date_created"] = $date_created;


    $response["message"] = "Loaded";
    array_push($response["sales"], $sale);

}

echo json_encode($response);



?>

Image showing both my files are in the same folder

Image showing the same query running in phpmyadmin

Image showing the file not displaying anything when ran in browser

1 个答案:

答案 0 :(得分:1)

终于想通了,我应该说非常愚蠢的错误,代码还可以,我的意思是逻辑,问题是我的变量命名,

我将bind中的$ comment声明为字段

$result->bind_result($id, $user_fk, $fname, $lname, $comment, $date_created);

然后我继续并错误地将其声明为托管查询产生的数据的数组,在它消耗的那天结束时:D感谢大家下次我会特别小心你的答案< / p>

$comment = array();
$comment["id"] = $id;
$comment["user_fk"] = $user_fk;
$comment["fname"] =$fname;
$comment["lname"] = $lname;  
$comment["comment"] = $comment;
$comment["date_created"] = $date_created;




 $result->bind_result($id, $user_fk, $fname, $lname, $comment, $date_created);
while($row = $result->fetch()) 
{
    $commentArray = array();
    $commentArray["id"] = $id;
    $commentArray["user_fk"] = $user_fk;
    $commentArray["fname"] =$fname;
    $commentArray["lname"] = $lname;  
    $commentArray["comment"] = $comment;
    $commentArray["date_created"] = $date_created;


    $response["message"] = "Loaded";
     $response["error"] = FALSE;
    array_push($response["comments"], $commentArray);

}`