简体   繁体   中英

SQL query running in PhpMyAdmin panel but not running in Php script

I have a query that is giving me a headache, its running when I put in phpmyadmin but not producing any output not even an error when ran put in a script, I cant say its the connection or anything, I have another file almost exactly as this one and it works flawlessly but not this one, at least even an error at this point would be appreciated, below is my script:

<?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);

?>

And my other script which is running perfectly

<?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);



?>

显示我的两个文件位于同一文件夹中的图像

显示在 phpmyadmin 中运行的相同查询的图像

显示文件在浏览器中运行时不显示任何内容的图像

Finally figured it out, very stupid mistake i should say, the code is ok, well the logic I mean, the issue was my variable naming,

I declared $comment in bind as a field

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

Then I went on and mistakenly declared it as the array that was to host the data produced by the query, at the end of the day it was consuming itself :D thanks everyone for you answers next time I will be extra careful

$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);

}`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM