-1

I have a boolean field, which seems to be stored as 1 when true.

When trying to grab that field in a while loop, I get the error:

mysql_fetch_array() expects parameter 1 to be resource, boolean given

Should a boolean be stored as "TRUE" instead of "1"?

Otherwise, why would I be getting this error?

edit:

My code:

$sqltotal = mysql_query("SELECT SUM(cost) as total FROM sales where passport = '{$therecord['passport']}' AND {$therecord['paid']} <> 1");
$row = mysql_fetch_array($sqltotal);
  • You should check the [mysql_query](http://php.net/manual/en/function.mysql-query.php) documentation. Quote: *mysql_query() returns a resource on success, or FALSE on error* This is what Hammerite and Artefacto are explaining. – Mike Jul 06 '10 at 10:07

2 Answers2

2

Your error unrelated to how booleans are stored, be it in the database or in PHP.

The first argument to mysql_fetch_array() must be the query you've opened executed with mysql_query; that's what the error says.

Artefacto
  • 93,596
  • 16
  • 191
  • 218
2

There are two things that might have happened here:

  1. You've attempted to carry out a query by using code similar to $resultset = mysql_query(...); but the response from MySQL was an error message, and $resultset contains Boolean FALSE.
  2. You've carried out a query that doesn't return a resultset, such as an INSERT or UPDATE query, and $resultset contains Boolean TRUE which just indicates that the query was successful.

While debugging, try the following:

if ( $resultset === false ) {
    echo mysql_error();
} else {
    // whatever you were doing
}

However, there's a danger of leaving this in production code, which will discloses too much information to non-admin users. Better would be to log the error and display a generic error message to the user.

if ( $resultset === false ) {
    $message = 'SQL Error in ' . __FILE__ . '@' . (__LINE__ - 2) . ': ' . mysql_error() . "\n";
    error_log($message, 3, ERRLOG); # ERRLOG must be defined
    # or send an e-mail to the developer
    //error_log($message, 1, $developer_email);
    # output an error message to the user. For example:
    ?>
    <p class="error">There was an internal database error. 
      It's been logged, and we'll look into it. Please try again later.
    </p>
    <?php
} else {
    // whatever you were doing
    ...
}

Even better than that, switch to PDO and use exceptions.

outis
  • 72,188
  • 19
  • 145
  • 210
Hammerite
  • 20,746
  • 4
  • 67
  • 87
  • Probably a really dumb question, but what would I use for $resultset? $row is only defined after I would use it, i.e. in the //whatever I was doing part... –  Jul 06 '10 at 10:02
  • You edited your question while I was writing my answer. The variable that plays the role of my `$resultset` in your code is `$sqltotal`. – Hammerite Jul 06 '10 at 10:13