Jump to content

PHP not returning FALSE value


JsusSalv

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/132778-php-not-returning-false-value/
Share on other sites

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

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.