“警告:mysql_fetch_array()期望参数1为资源,给定布尔值"尝试创建php购物车时出错 [英] "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given" error while trying to create a php shopping cart

查看:98
本文介绍了“警告:mysql_fetch_array()期望参数1为资源,给定布尔值"尝试创建php购物车时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
PHP错误:mysql_fetch_array()期望参数1是资源,给定布尔值

Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given

这是我在product.php中拥有的代码,每次我从主页上单击某个产品时,它都会显示以下警告:

This is the code I have in my product.php, every time I click on a product from the home page it comes up with the warning below:

if ( isset( $_GET['ID'] ) ) {
    $product_id = $_GET['ID'];
    $query = "SELECT Name, Genre, Price, Year, Picture FROM Products";
    $result = mysql_query( $query );

    while ( $row = mysql_fetch_array( $result, MYSQL_NUM ) ) {
        echo "<div><p>Name: $row[0]</p><p>Genre: $row[1]</p><p>Price: $row[2]</p><p>Year: $row[3]</p></div>";
    }
    echo "<div><a href=\"cart.php?action=add&product=$product_id\">add to basket</a></div>";
}

我得到警告:

警告:mysql_fetch_array()希望参数1为资源,在第12行的/Applications/XAMPP/xamppfiles/htdocs/cart/product.php中给出布尔值

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/cart/product.php on line 12

推荐答案

Year是mysql保留字,您必须使用反引号将其转义``

Year is a mysql reserved word you have to escape it using backticks ``

$query = "SELECT Name, Genre, Price, `Year`, Picture FROM Products";

您必须使用某种如下所示的mysql错误检查

you must use some kind of mysql error checking like below one

$result = mysql_query($query) or trigger_error(mysql_error());

这篇关于“警告:mysql_fetch_array()期望参数1为资源,给定布尔值"尝试创建php购物车时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆