PHP Warning MySQL_Fetch_Assoc

how to prevent this error : Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in ... on line 11

Here's the proper way to do things:

<?PHP
$sql = 'some query...';
$result = mysql_query($q);

if (! $result){
throw new My_Db_Exception('Database error: ' . mysql_error());
}

while($row = mysql_fetch_assoc($result)){
//handle rows.
}

Note the check on (! $result) -- if your $result is a boolean, it's certainly false, and it means there was a database error, meaning your query was probably bad.

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

$rsetCoupons is not a mysql query resource at that point in execution. Either your query is failing, or the variable is being lost somewhere.

http://php.net/mysql_fetch_assoc

I'm fairly certain you're query is failing. You should check the return of mysql_query and if it's false, then check mysql_error().

Also, you should not suppress errors in your mysql_connect and mysql_select_db calls. If the database connection cannot be made, you should handle that more gracefully than letting your page trod on and error on every subsequent mysql call. That may actually be what your error is. If you're suppressing errors to hide them from users, public facing PHP sites should have display_errors set to off, but you should still be logging errors.

Error Warning: mysql_fetch_assoc() in php

Try to find what is the error:

  function getData() {
$data = array();
$sql = 'Select * From test';
$query = mysql_query($sql);
if(!$query)
{
echo 'Error: ' . mysql_error(); /* Check what is the error and print it */
exit;
}

while($row = mysql_fetch_array($query)) { /* Better use fetch array instead */
$data[] = array($row['id'], $row['name']);
}
return $data;
}

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result

write like this:

$sql_select="select route from $client_id WHERE net_id = ".$rownum."";
$queryRes = mysql_query($sql_select);
print $sql_select;
print mysql_error();
$i=1;
while($rows=mysql_fetch_assoc($queryRes))

You need to provide #Resource returned from mysql_query() function to mysql_fetch_assoc().

Note: Mysql_* are deprecated from PHP5.3. Hence should be avoided.

Rate system warning mysql_fetch_assoc?

Error means that your query failed.

Because when a query fails, mysql_query() returns false and false, as I think you know, is a boolean..

Well, also you are using mysql syntax that is deprecated.

I suggest you to use pdo or mysqli.

Also I don't see connections to database...

If you aren't connected to any db, mysql_query returns false.

Then, the code that you shown to us is completed? Or you are missing any things like connection, select db and others?

If the answer to my question is yes then:

Add these simple code lines before your actual code:

$conn = mysql_connect('localhost', 'username', 'password');

if (!$conn) {
die ('Error while attempting connection to db: ' . mysql_error());
}

$db_selected = mysql_select_db('test', $conn);
if (!$db_selected) {
die ("Error while selecting db: " . mysql_error());
}

If the answer to my question is no, please edit your post adding ALL code lines.



Related Topics



Leave a reply



Submit