Today we met arning: mysql_fetch_assoc () expects parameter 1 to be resource, boolean given in. Let's take a look at the solution.
I encountered this error when I learned php today:
Warning: mysql_fetch_assoc () expects parameter 1 to be resource, boolean given in C: xampphtdocsmyblogindex. php on line 15
Source code:
| The Code is as follows: |
Copy code |
|
<? Php
$ SQL = "select entries. *, categories. cat from entries, categorie where entries. cat_id = categories. id order by dateposted desc limit 1 ;";
$ Result = mysql_query ($ SQL );
$ Row = mysql_fetch_assoc ($ result );
Echo "Echo "<I> in <a href = 'viewcat. php? Id = ". $ row ['cat _ id']. "'> ". $ row ['cat']. "</a>-Posted on ". date ("D js f y g. iA ", strtotime ($ row ['dateposted']). "</I> ";
Echo "<p> ";
Echo nl2br ($ row ['body']);
Echo "</p> "; |
Baidu finds a solution! The error occurs because the musql_fetch_assoc () function returns false because there is no data in the database. Therefore, the following $ row [''] error occurs!
Therefore, when using the mysql_fetch_assoc () function, first judge $ result!
The Code is as follows:
| The Code is as follows: |
Copy code |
|
<? Php
$ SQL = "select entries. *, categories. cat from entries, categorie where entries. cat_id = categories. id order by dateposted desc limit 1 ;";
$ Result = mysql_query ($ SQL );
If ($ result ){
$ Row = mysql_fetch_assoc ($ result );
Echo "Echo "<I> in <a href = 'viewcat. php? Id = ". $ row ['cat _ id']. "'> ". $ row ['cat']. "</a>-Posted on ". date ("D js f y g. iA ", strtotime ($ row ['dateposted']). "</I> ";
Echo "<p> ";
Echo nl2br ($ row ['body']);
Echo "</p> ";
}
Else {
Echo "no articles ";
}
?> |
In this way, no error is reported!
Certificate ----------------------------------------------------------------------------------------------------------------------------------
Note: mysql_fetch_assoc () function
Definition and usage
The mysql_fetch_assoc () function retrieves a row from the result set as an associated array.
Returns the associated array generated based on the rows obtained from the result set. If no more rows exist, false is returned.
Syntax
Mysql_fetch_assoc (data) parameter description
Data is required. The Data Pointer to use. The Data Pointer is the result returned from mysql_query.
Tips and comments
Note: mysql_fetch_assoc () is identical to mysql_fetch_array () and the second optional parameter MYSQL_ASSOC. It only returns the associated array. This is also the initial working method of mysql_fetch_array.
Tip: If you need a numeric index in addition to the associated index, use mysql_fetch_array ().
Note: The field names returned by this function are case sensitive.