2

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

27
  • MySQL connection has been established or not?? Commented Oct 17, 2016 at 11:50
  • Yes, look at the second script, everything is the same except for the query and variables Commented Oct 17, 2016 at 11:53
  • Are the files in the same folder? Commented Oct 17, 2016 at 11:55
  • what error you are getting can you please post here? Commented Oct 17, 2016 at 11:56
  • 1
    In phpmyadmin, do you receive any output? I mean are there any rows returned? Commented Oct 17, 2016 at 11:58

1 Answer 1

1

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

}`
Sign up to request clarification or add additional context in comments.

1 Comment

So obvious now you've found it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.