So I'm getting this error message from MYSQL when I load this page. The first message is what's it displaying on the PHP code, the 2nd message is the result of mysql_error().

(1) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in [I][redacted][/I] on line 19

(2) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND CigarID = 334 AND DATE_SUB(CURDATE(),INTERVAL 15 DAY) <= DateAdded' at line 1

When I run the query in mySQL it returns a result, as it should. So I'm not sure what's going on here. I've check to make sure the variable wasn't recycled and it's not. Any ideas what's going on here? Here's the offending code --

//Check User's Last Review
$SQL = "SELECT COUNT(CigarID) FROM reviews_cigar WHERE UserID = $UserID AND CigarID = $CigarID AND DATE_SUB(CURDATE(),INTERVAL 15 DAY) <= DateAdded";
$Result = mysql_query($SQL);
$row = mysql_fetch_array($Result) or die(mysql_error());
if( $row[0] > 0 ){
$errormsg = "Sorry but you have submitted a review for this Cigar in the past 15 days";

Dani AI

Generated

Good diagnosis and follow-up from and — the runtime warning is a secondary symptom: mysql_query returned false (boolean) because the SQL failed, and mysql_fetch_array then complained because it expected a resource. That pattern shows up a lot: an SQL syntax/quoting problem or an uninitialised variable will make the query fail, and the fetch call only reveals the problem indirectly.

Quick checklist for troubleshooting (fast, practical checks that work years later):

  • Capture the exact SQL string and the variable types: log or echo the final SQL and do var_dump() on any variables used in WHERE.
  • Immediately check the DB error: call mysql_error() / mysql_errno() right after mysql_query and log both the error and the SQL.
  • Verify types and quoting: numeric columns should be cast (intval), string columns must be wrapped and escaped (mysql_real_escape_string or prepared statements). Uninitialised variables commonly produce syntax like WHERE Region= which breaks SQL.
  • Watch for reserved words and malformed functions (DATE_SUB placement, missing parentheses, etc.).
  • Avoid calling fetch functions until confirming the query succeeded.

Short examples (do not repeat code from thread):

/* quick-safety pattern with old mysql_* */
$uid = isset($UserID) ? intval($UserID) : 0;
$sql = "SELECT COUNT(*) AS cnt FROM reviews_cigar WHERE UserID = $uid AND CigarID = ".intval($CigarID);
$res = mysql_query($sql);
if (!$res) {
  error_log('SQL failed: '.mysql_error().' | '.$sql);
} else {
  $row = mysql_fetch_assoc($res);
  if ($row['cnt'] > 0) { /* handle logic */ }
}
/* preferred: PDO with prepared statements */
$st = $pdo->prepare('SELECT COUNT(*) FROM reviews_cigar WHERE UserID = :u AND CigarID = :c AND DATE_SUB(CURDATE(), INTERVAL 15 DAY) <= DateAdded');
$st->execute([':u'=>$UserID, ':c'=>$CigarID]);
$count = $st->fetchColumn();

Note: mysql_* functions were deprecated and later removed from modern PHP; migrating to mysqli or PDO with prepared statements avoids this class of error and prevents SQL injection. For the likely fix was quoting/escaping the Region value; for , using column names or aliases instead of fragile index-based ORDER BY (order by 4) makes code clearer and more robust.

Recommended Answers

All 4 Replies

mysql_query returns a boolean false when the query fails so if you fix the query (to which the second error applies) the first will disappear also.

As for the second error: it's probably something to do with $UserID as the rest looks alright.
Maybe you could show us what the query looks like when it's executed? (echo $SQL)

mysql_query returns a boolean false when the query fails so if you fix the query (to which the second error applies) the first will disappear also.

As for the second error: it's probably something to do with $UserID as the rest looks alright.
Maybe you could show us what the query looks like when it's executed? (echo $SQL)

That was it, I forgot to bring over the $UserID variable - doh! :|

Thanks for the help!

I Have a problem with syntax error on the following code

$myregion = $_POST['region'];
echo $myregion;
$names = mysql_query("SELECT * FROM course_name WHERE Region=$myregion");

while($row = mysql_fetch_array($names)) {
echo $row['Name_of_Course'], " " , $row['Region'], " " , $row['Price'],"<br>";
}

error is fetch_array expectects parameter 1 to be resource, boolean given on line 47

This code worked fine if I hard coded the Where condition but once I have put the $variable in, i get the error?

Could nyone help as I am new to php mysql

$query = "SELECT Name,Mobile,Website,Rating FROM grand_table order by 4";


// Passing Variables to execute and check

$result = mysql_query($query);

while( $data = mysql_fetch_array($result))
{
  echo("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td></tr>");

 }

To solve up the parameter and boolean error i had used above query

Also you can parse up value directly in the query ...

If you had any query you can contact me at : http://www.gfxdevelopers.com , My Id there : Gears.of.Codes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.