WARNING:MYSQL_FETCH_ASSOC () expects parameter 1 to be resou_php tutorial

Source: Internet
Author: User
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: ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.