Thandi Ngamu
Thandi Ngamu

Reputation: 87

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



?>

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

Upvotes: 2

Views: 630

Answers (1)

Thandi Ngamu
Thandi Ngamu

Reputation: 87

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

}`

Upvotes: 1

Related Questions