Jump to content

SQL Query and Variable help


redgunner

Recommended Posts

:shrug: :shrug: :shrug:

 

	$id = '9';

$MyQuery = "SELECT * FROM bookings WHERE id = '$id'";
$retrieve = mysql_query($MyQuery) or die(mysql_error());
if(mysql_num_rows($retrieve) != 0):
	$row = mysql_fetch_assoc($retrieve);
else:
	echo 'No Information';
endif;	

echo 'Full Name: ' . ($row['fullname']) . '<br><br>';	

 

The above code is working for me however when I change it to what I do...

 

	$id = ($row['id']);

 

It tells me I have an undefined row variable... What will fix this? Thanks in advance...

Link to comment
Share on other sites

Because the $row variable is being created within a conditional statement, it will only be created IF that statement is true.

In the case, the $row variable will only be set IF  the numbers of rows DOES NOT equal zero. Obviously, the query is returning 0 and not setting your $row variable. <-- Ignore that, I misread.

 

Its because you're referencing the $row variable before the $row variable even exists. The $row variable isn't defined until here:

$row = mysql_fetch_assoc($retrieve);

 

After that, assuming that the conditional statement succeeds, you can use it. Otherwise, if a variable has not been set/defined/exists prior to use, PHP will return an error.

Link to comment
Share on other sites

Tried that and I get this...

 

Notice: Undefined index: id in C:\Users\Chris\Desktop\www\edit.php on line 9

No Information

Notice: Undefined variable: row in C:\Users\Chris\Desktop\www\edit.php on line 19

ID:

 

the previous page has

<a href="edit.php?id.php='.$row['id'].'">edit</a>

 

Im totally stuck on this1 and ive tried everything :(

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.