Home > Backend Development > PHP Tutorial > Warning: mysql_fetch_assoc() expects parameter 1 to be resou_PHP教程

Warning: mysql_fetch_assoc() expects parameter 1 to be resou_PHP教程

WBOY
Release: 2016-07-13 10:55:44
Original
1435 people have browsed it

I encountered the arning today: 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 was learning PHP today:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:xampphtdocsmyblogindex.php on line 15

The source code is:

The code is as follows
 代码如下 复制代码

 

$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 "

";

Copy code

$sql="select entries.*,categories.cat from entries,categorie where entries.cat_id=categories.id order by dateposted desc limit 1;";

$result=mysql_query($sql);
代码如下 复制代码

$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 "没有文章";
 }

?>

        $row=mysql_fetch_assoc($result);

echo "

" . $row['subject'] . "< /h2>
";

echo " in " . $row['cat'] . " - Posted on " . date("D js F Y g.iA",strtotime($row['dateposted'])) . "";

echo "

";

echo nl2br($row['body']);

echo "

";


After searching on Baidu, I found a solution! The reason for his error is that there is no data in the database and the musql_fetch_assoc() function returns false, so the use of $row[''] below is wrong!

So when using the mysql_fetch_assoc() function, make a judgment on $result first!

The code is as follows:

if($result){         $row=mysql_fetch_assoc($result);
The code is as follows

Copy code

$sql="select entries.*,categories.cat from entries,categorie where entries.cat_id=categories.id order by dateposted desc limit 1;";

$result=mysql_query($sql);
echo "

" . $row['subject'] . "< /h2>
"; 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 way there will be no errors! -------------------------------------------------- -------------------------------------------------- ---------------------------------- Note: mysql_fetch_assoc() function Definition and usage The mysql_fetch_assoc() function fetches a row from the result set as an associative array. Returns an associative array based on the rows taken 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(). Tips and Notes Note: mysql_fetch_assoc() is exactly the same as using mysql_fetch_array() plus the second optional parameter MYSQL_ASSOC. It just returns an associative array. This is also how mysql_fetch_array() initially works. Tip: If you need a numeric index in addition to a relational index, use mysql_fetch_array(). Note: The field names returned by this function are case-sensitive.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632214.htmlTechArticleI encountered arning today: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in, let’s take a look Find a solution. I encountered this error when I was learning PHP today:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template