Primary key field wont retrieve

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imperium2335
Forum Newbie
Posts: 10
Joined: Fri Mar 20, 2009 9:24 pm

Primary key field wont retrieve

Post by imperium2335 »

Hi, I'm trying to retrieve the information from my table but it returns a blank page.

image_id is the primary key in the table and is set to auto increment, is this why i cant retrieve the info? Ive retrieved info from other fields with no problems.

Code: Select all

<?PHP
 
if (!file_exists("../dbconnect.php"))
{
    die("Database settings not found, administrator intervention required.") ;
}
else
{
    require("../dbconnect.php") ; //Must connect to the database.
}
 
//$userrate = $_GET['rating'] ;
$currentimage = 5 ;//$_SESSION['currentimage'] ;
 
$query = "SELECT image_id FROM image_bank WHERE image_id = '$currentimage'" ;
 
$result = mysql_query($query) ;
 
$nrows = mysql_fetch_row($result) ;
 
while($row = mysql_fetch_array($result))
{
 
    $currentrating = $row['rating'] ;
    $numratings = $row['numratings'] ;
    
echo "is $currentrating $numratings" ;
 
}
?>
Thanks in advance.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Primary key field wont retrieve

Post by requinix »

A blank page or just the word "is"?
imperium2335
Forum Newbie
Posts: 10
Joined: Fri Mar 20, 2009 9:24 pm

Re: Primary key field wont retrieve

Post by imperium2335 »

the word is, sorry.

Ive echoed the query and it shows SELECT * FROM image_bank WHERE image_id = 5 which is correct i think.

Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Primary key field wont retrieve

Post by requinix »

The query is only supposed to return one row, right?

Code: Select all

$nrows = mysql_fetch_row($result);
 
while($row = mysql_fetch_array($result))
In the first line you fetch the row. The only row. There's nothing left to get after that.
imperium2335
Forum Newbie
Posts: 10
Joined: Fri Mar 20, 2009 9:24 pm

Re: Primary key field wont retrieve

Post by imperium2335 »

Not sure i understand what you mean, I want it to return whats in the numratings and rating fields that are in my database.
imperium2335
Forum Newbie
Posts: 10
Joined: Fri Mar 20, 2009 9:24 pm

Re: Primary key field wont retrieve

Post by imperium2335 »

never mind, i deleted that line and it works now lol thanks. Actually i was kinda dumb, why try count rows when i know only one will be retrieved lol :P
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Primary key field wont retrieve

Post by requinix »

If you want to count rows then mysql_num_rows is the function you want.
Post Reply