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
https://forums.phpfreaks.com/topic/211847-sql-query-and-variable-help/
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.

You can't use the variable before it's defined, therefore you can't use it as part of the WHERE clause in that query before the query has even run. Please explain exactly what you're trying to do with this code.

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

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.