0
$min_range=$_GET['min_range'];
$max_range=$_GET['max_range'];
$brand_name=$_GET['brand'];

$result=mysql_query(
    "
        SELECT 
            brand,
            image,
            price
        FROM 
            mobile
        WHERE 
            brand = '"$brand_name " 'price =3000
    "
);

我想在我的 sql 查询中使用 php 变量。这段代码有什么问题。以及如何纠正。

它说“解析错误:语法错误,意外('$brand_name')”

4

6 回答 6

1

一种方法如下:

$result=mysql_query(
    "
        SELECT 
            brand,
            image,
            price
        FROM 
            mobile
        WHERE 
            brand = '" . $brand_name . "'
        AND
            price =3000
    "
);

另一种方法是:

$result=mysql_query(
    "
        SELECT 
            brand,
            image,
            price
        FROM 
            mobile
        WHERE 
            brand = '$brand_name'
        AND
            price = 3000
    "
);

另一种方式是这样的:

$result=mysql_query(
    sprintf(
        "
            SELECT 
                brand,
                image,
                price
            FROM 
                mobile
            WHERE 
                brand = '%s'
            AND
                price = 3000
        ",
        $brand_name
    )
);

另请注意,您的代码带有语法错误,请将 AND 运算符替换为满足您需求的运算符。

于 2013-11-04T07:20:46.557 回答
0
$result=mysql_query(
    "
        SELECT 
            brand,
            image,
            price
        FROM 
            mobile
        WHERE 
            brand = '.$brand_name.' AND price =3000
    "
);
于 2013-11-04T07:34:28.573 回答
0

将您的查询更改为

$result=mysql_query("SELECT brand,image,price FROM mobile WHERE brand='$brand_name' AND price=3000");

您实际上忘记了将条件分开。添加了一个AND关键字。

于 2013-11-04T07:19:08.413 回答
0

$result = $mysql->prepare("SELECT brand, image, price FROM mobile WHERE brand =? AND price=3000");

$result->bind_param($brand_name);

$result->execute();

于 2013-11-04T08:04:15.970 回答
0

你可以试试这个:

    $result=mysql_query(
    "
        SELECT brand, image, price
        FROM mobile
        WHERE 
            brand = '" . $brand_name . "' AND price = 3000
     ");
于 2013-11-04T07:57:35.917 回答
0
$result = mysql_query("SELECT brand,image,price FROM mobile WHERE brand = '$brand_name' AND price = 3000");
于 2013-11-04T07:22:06.083 回答