Jump to content

Reading data from single row in database


hellonoko

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/82050-reading-data-from-single-row-in-database/
Share on other sites

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

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

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.