Warning: mysql_fetch_assoc () expects parameter 1 to be resou

Source: Internet
Author: User

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.

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.