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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.