Jump to content

variables into sql query from previous sql query


real_illusions

Recommended Posts

Hi all,I'm trying to use variables inside an sql query, depending on a previous sql query.With the first query, that works all ok, the data is pulled through etc etc...Ive set up a variable to fetch a certain value from that table, which is the id of the latest entry.I then use another query for another table on the same database, where that variable is used. However, i cant get it to work.I've echoed the variable after ive set it (and before the 2nd sql query), and its working fine, its reading the correct data. However, when put into the 2nd query for the other database table, nothing is pulled through.

$qry = mysql_query("SELECT * FROM articletable ORDER BY id DESC")or die ("select died: " . mysql_error());// get the most recent 3 entriesfor($i = 0; $i < 3; $i++)$answer[$i] = mysql_fetch_array($qry);// get the article id of the most recent entry$articleid = $answer[0]['article_id'];// query the 2nd table for the comments section, this is where it all goes wrong$data = mysql_query("SELECT * FROM comments_table WHERE article_id =" . $articleid . " ORDER BY id DESC ")or die ("select died:" . mysqlerror());while($row = mysql_fetch_array($data)){}

I get no errors, ive tried with and without " and the . and various combinations. Just the data isnt been.Adding the following line, inside and after the {} of the whileecho $row['comments'];doesnt echo anything. I've checked all the names and such match up with whats in the database correctly. So i dont know whats wrong... :)

Link to comment
Share on other sites

Are you sure display_errors is on? If not, there may be errors that are not SQL errors, but PHP errors. Are you sure the while() is even executed?First, add

ini_set('display_errors', 'On');error_reporting(E_ALL | E_STRICT);

at the top, just to make sure you're seeing all errors.Next, var_dump as much stuff as you can. Wherever goes unexpected, that's the point of error. That is:

$articleid = $answer[0]['article_id'];var_dump($answer, $articleid);// query the 2nd table for the comments section, this is where it all goes wrong$data = mysql_query("SELECT * FROM comments_table WHERE article_id =" . $articleid . " ORDER BY id DESC ")or die ("select died:" . mysqlerror());var_dump($data);while($row = mysql_fetch_array($data)){var_dump($row);}

The beauty of var_dump() is that it will show you the type and value of anything, unlike echo which will show nothing if the variable is null or false.

Link to comment
Share on other sites

Wha'? This can't be right... while() shoudn't even be executed if $row is bool(false).What bool(false) means is that there are no more results to show (or there weren't any to begin with), but

while($row = mysql_fetch_array($data))

means that

var_dump($row);

will only be executed if $row evaluates to bool(true). And bool(false) is one thing (probably THE only thing) which NEVER in NO VERSION of ANY LANGUAGE evaluates to bool(true).Try to remove the

or die ("select died:" . mysqlerror())

just for testing's sake. Or better yet, add it below the query (and after you remove the above):

echo 'MySQL error(s): ';var_dump(mysqlerror());

Link to comment
Share on other sites

Figured it out..turns out there wasn't anything in the database that matched the variable in the 2nd sql query. If it found data in the database, then it works fine. Worked it out by replacing the variable in the 2nd sql query with an actual number that would or wouldn't match the database and see what happens.Added a couple of other lines in case theres nothing in the database.

$qry = mysql_query("SELECT * FROM articletable ORDER BY id DESC")or die ("select died: " . mysql_error());// get the most recent 3 entriesfor($i = 0; $i < 3; $i++)$answer[$i] = mysql_fetch_array($qry);// get the article id of the most recent entry$articleid = $answer[0]['article_id'];// query the 2nd table for the comments section$data = mysql_query("SELECT * FROM comments_table WHERE article_id = " . $articleid . " ORDER BY id DESC ")or die ("select died: " . mysql_error());if ( mysql_num_rows( $data ) > 0 )while($row = mysql_fetch_array($data)){$firstComment =  $row['comments'];}else {$firstComment =  "Nothing yet!";}

I put the output as variables so i can control where they appear on the page.:)Does that explain the bool(false) thing or not?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...