JsusSalv Posted November 15, 2008 Share Posted November 15, 2008 Hello Everyone: Could someone look over my code and tell me what I'm doing wrong? The code successfully returns TRUE values but if the mysql resource returns FALSE it does NOT display the error message. I've looked at a ton of examples this morning and am just banging my head over this simple bit. Please help? The code is just the main bits, no security checks or validations are copied here for readability purposes. I've checked the php.net page, http://us2.php.net/mysql_query, example #2 fits the best. I THINK my code looks like the example but it's not working. Any fresh pair of eyes that can help? Thank you! ... if ($webpage_table) { $sql = "SELECT * FROM $webpage_table"; $result = mysql_query($sql) or die (mysql_error()); } } ?> <div id="contentSection"> <?php if (!$result) { echo '<p class="warning">Invalid request: record does not exist.</p>'; echo 'MySQL Error: ' . mysql_error(); exit; } else { while($row = mysql_fetch_array($result)) { ... ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/132778-php-not-returning-false-value/ Share on other sites More sharing options...
DeanWhitehouse Posted November 15, 2008 Share Posted November 15, 2008 Because if there is an error the code is stopped before the if statement $result = mysql_query($sql) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/132778-php-not-returning-false-value/#findComment-690522 Share on other sites More sharing options...
JsusSalv Posted November 15, 2008 Author Share Posted November 15, 2008 Ok, I've removed that bit: or die (mysql_error()); However, I'm still not getting the error message. Is there a better way to write this? Quote Link to comment https://forums.phpfreaks.com/topic/132778-php-not-returning-false-value/#findComment-690523 Share on other sites More sharing options...
kenrbnsn Posted November 15, 2008 Share Posted November 15, 2008 That's not how you check whether the query returned any results. The mysql_query call will return false if there is a problem with the query. If there are no rows to get, then the query succeeds, but it returns no rows, so you need to use the mysql_num_rows function to check to see how many rows were retrieved. <?php ... if ($webpage_table) { $sql = "SELECT * FROM $webpage_table"; $result = mysql_query($sql) or die (mysql_error()); } } ?> <div id="contentSection"> <?php if (mysql_num_rows($result) == 0) { echo '<p class="warning">Invalid request: record does not exist.</p>'; } else { while($row = mysql_fetch_array($result)) { ... ?> </div> Ken Quote Link to comment https://forums.phpfreaks.com/topic/132778-php-not-returning-false-value/#findComment-690525 Share on other sites More sharing options...
JsusSalv Posted November 15, 2008 Author Share Posted November 15, 2008 Awesome!!! That rocks. I suppose another blue star is in order for you. Thank you both for the help. Much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/132778-php-not-returning-false-value/#findComment-690530 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.