redgunner Posted August 27, 2010 Share Posted August 27, 2010 :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... Quote Link to comment https://forums.phpfreaks.com/topic/211847-sql-query-and-variable-help/ Share on other sites More sharing options...
Vince889 Posted August 27, 2010 Share Posted August 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/211847-sql-query-and-variable-help/#findComment-1104237 Share on other sites More sharing options...
redgunner Posted August 27, 2010 Author Share Posted August 27, 2010 Yeah but everytime I go to define the $row variable I still need to pickup on $id and is really confusing me... Could I be so bold to ask what a working code would be? Quote Link to comment https://forums.phpfreaks.com/topic/211847-sql-query-and-variable-help/#findComment-1104243 Share on other sites More sharing options...
Pikachu2000 Posted August 27, 2010 Share Posted August 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/211847-sql-query-and-variable-help/#findComment-1104344 Share on other sites More sharing options...
redgunner Posted August 27, 2010 Author Share Posted August 27, 2010 The page is edit.php?id=x x being unique primary key I just need $id variable to set it to whatever that number is... Quote Link to comment https://forums.phpfreaks.com/topic/211847-sql-query-and-variable-help/#findComment-1104387 Share on other sites More sharing options...
Pikachu2000 Posted August 27, 2010 Share Posted August 27, 2010 Oh, that's easy enough . . . $id = (int) $_GET['id']; // typecast to integer when the incoming value is expected to be an integer . . . Quote Link to comment https://forums.phpfreaks.com/topic/211847-sql-query-and-variable-help/#findComment-1104388 Share on other sites More sharing options...
redgunner Posted August 27, 2010 Author Share Posted August 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/211847-sql-query-and-variable-help/#findComment-1104416 Share on other sites More sharing options...
Pikachu2000 Posted August 27, 2010 Share Posted August 27, 2010 Would you explain exactly what it is you're trying to do, and in what order things are currently being done? That would make it a lot easier to help you with this. Quote Link to comment https://forums.phpfreaks.com/topic/211847-sql-query-and-variable-help/#findComment-1104464 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.