PHP SQL 错误资源#6

标签 php mysql sql

这是我的代码:

//Connect to config file
include(dirname(__FILE__)."/../config.php");
//Connect to the database
$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

//Get Total Products
$result = mysql_query("SELECT * FROM $dbProductsTable WHERE sub_cat = '$subcat'");
$TotalProducts = mysql_num_rows($result);

//Create Pages
if (TotalProducts <= 12){
    $pages = '';
}else{
    $pages = "                  <ul id=\"pagination\" class=\"group\">
                        <li><a class=\"current\" href=\"#\">1</a></li>
                        <li><a href=\"#\">2</a></li>
                        <li><a href=\"#\">3</a></li>
                        <li><a href=\"#\">4</a></li>
                    </ul>";
}




//Get from sql info we need
$sql = mysql_query("SELECT * FROM $dbProductsTable WHERE sub_cat = '$subcat' LIMIT 0 , 12");
$data = mysql_query($sql) or die(mysql_error());

//Make first letter UpperCase
$subcatname = ucfirst($subcat);
//Get Basket Count
$basketcount = count($_COOKIE['products']);

//Get All Products
while($row = mysql_fetch_array($data))
{
//Get Product Path
$productPath = $Domain.'/'.strtolower($row['category']).'/'.str_replace(" ","",$row['product_name']).'_'.$row['product_id'];

//Build Products List
$products = $products."     <li class=\"all-products-list-item\">
            <a href=\"$productPath\" title=\"{$row['product_name']}\"><img src=\"$ProductImageFolder{$row['thumb_image']}\" alt=\"{$row['product_name']}\" border=\"0\" height=\"245\" width=\"180\"/></a>
            <h3><a href=\"$productPath\">{$row['product_name']}</a></h3>
            <p>£{$row['price']}</p>
        </li>\n";
}

这是错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #6' at line 1

我尝试通过 phpmyadmin 运行它,并且 sql 工作完美,我找不到问题是什么?

谢谢

最佳答案

这里是错误:

//Get from sql info we need
$sql = mysql_query("SELECT * FROM $dbProductsTable WHERE sub_cat = '$subcat' LIMIT 0 , 12");
$data = mysql_query($sql) or die(mysql_error());

更改为

//Get from sql info we need
$sql = "SELECT * FROM $dbProductsTable WHERE sub_cat = '$subcat' LIMIT 0 , 12";
$data = mysql_query($sql) or die(mysql_error());

您正在使用$sql = mysql_query(...)运行查询,并将资源分配给变量$sql。之后,您尝试使用变量 $sql 作为参数来运行查询。此时,$sql 将包含资源,而不是查询。

关于PHP SQL 错误资源#6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6844276/

相关文章:

php - 如何在局域网上部署 PHP 应用程序?

php - 带有 codeigniter 的 Oracle to_date 函数

php - 始终使用值获取 $media

php - Delphi DEC库(Rijndael)加密

php - 使用 PHP 添加到 MySQL 会添加两次并出现外部 ID 约束问题

sql - 社交网络查询之间的区别

mysql - SQL 在另一个 groupby 之上做一个 groupby

SQL Server - [SELECT] 是否锁定 [UPDATE]?

mysql - 结果中 2 列的聚合

sql - 使用特定列或字母顺序按查询排序。 - 冷聚变