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

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.