Today I met Arning:mysql_fetch_assoc () expects parameter 1 to be resource, boolean given in, let's take a look at the solution.
Today, when I was learning PHP, I encountered this error:
WARNING:MYSQL_FETCH_ASSOC () expects parameter 1 to BES resource, boolean given in c:xampphtdocsmyblogindex.php on line 15
The source code is:
|
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 ". $row [' Subject ']. " "; echo " in". $row [' Cat ']. "-Posted on". Date ("D js F Y G.ia", Strtotime ($row [' dateposted '])). ""; echo " "; Echo nl2br ($row [' body ']); echo " "; |
Baidu a bit, found a solution! The reason for his error is that there is no data in the database causing the MUSQL_FETCH_ASSOC () function to return a value of false, so the following $row['] use is wrong!
So when you use the MYSQL_FETCH_ASSOC () function, first Judge $result!
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 " ". $row [' Subject ']. " "; echo " in". $row [' Cat ']. "-Posted on". Date ("D js F Y G.ia", Strtotime ($row [' dateposted '])). ""; echo ""; Echo nl2br ($row [' body ']); echo " "; } else{ echo "no article"; } ? |
This will not be an error!
--------------------------------------------------------------------------------------------------------------- -------------------
Note: the MYSQL_FETCH_ASSOC () function
Definition and usage
The MYSQL_FETCH_ASSOC () function obtains a row from the result set as an associative array.
Returns an associative array based on the rows obtained from the result set, or False if there are no more rows.
Grammar
MYSQL_FETCH_ASSOC (data) parameter description
Data required. The data pointer to use. The data pointer is the result returned from mysql_query ().
Hints and Notes
Note: Mysql_fetch_assoc () and mysql_fetch_array () plus the second optional parameter Mysql_assoc exactly the same. It simply returns an associative array. This is also the initial working mode of mysql_fetch_array ().
Tip: If you need a numeric index outside of the associated index, use Mysql_fetch_array ().
Note: The field names returned by this function are case-sensitive.
http://www.bkjia.com/PHPjc/632214.html www.bkjia.com true http://www.bkjia.com/PHPjc/632214.html techarticle Today I met Arning:mysql_fetch_assoc () expects parameter 1 to be resource, boolean given in, let's take a look at the solution. Today, when I was learning PHP, I encountered this error: ...