hellonoko Posted December 17, 2007 Share Posted December 17, 2007 My following code that should select one row from my database does not seem to retrieve any information. Any ideas? Thanks in advance, $query = "SELECT id FROM ideas WHERE rank < $id_rank LIMIT 1" or die (mysql_error());; mysql_query($query) or die (mysql_error()); $row = mysql_fetch_row($result) or die (mysql_error()); $moving_idea_id = $row->id; // moves item rank DOWN one. echo $moving_idea_id; exit(); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 17, 2007 Share Posted December 17, 2007 I would us the mysql_fetch_assoc() function here. Also, you didn't save the result of the mysql_query() function. <?php $query = "SELECT id FROM ideas WHERE rank < $id_rank LIMIT 1" or die (mysql_error());; $result = mysql_query($query) or die (mysql_error()); $row = mysql_fetch_assoc($result) or die (mysql_error()); $moving_idea_id = $row['id']; // moves item rank DOWN one. echo $moving_idea_id; exit(); ?> Ken Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 17, 2007 Share Posted December 17, 2007 Adding to what Ken provided, when just pulling one or two fields, you can use LIST to quickly populate a variable to use. Please also note use of back ticks around field names and table names, single quotes around values. You also don't need so many 'or die()' conditions (a die() condition after just building the query, for example, is useless. It would never get triggered...). Once you have a result, only the freakiest of freakish incidents would cause an error for the data being requested (MySQL has already verified it has something for you). Instead of die(), you should test $result for a row with mysql_num_rows > 0 or similar. No need to kill off the script... <?php $query = "SELECT `id` FROM `ideas` WHERE `rank` < '$id_rank' LIMIT 1"; $result = mysql_query($query) or die (mysql_error()); list($moving_idea_id) = mysql_fetch_assoc($result); // moves item rank DOWN one. echo $moving_idea_id; exit(); ?> PhREEEk Quote Link to comment Share on other sites More sharing options...
hellonoko Posted December 17, 2007 Author Share Posted December 17, 2007 Thanks got it working.. I think. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.