Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我能';我不知道如何在SQL中使用$where来实现所选类别的功能_Php_Mysql_Sqlyog - Fatal编程技术网

Php 我能';我不知道如何在SQL中使用$where来实现所选类别的功能

Php 我能';我不知道如何在SQL中使用$where来实现所选类别的功能,php,mysql,sqlyog,Php,Mysql,Sqlyog,我想显示从表中选择的查询 <?php $where = ""; if(isset($_GET['category'])) { $catid = $_GET['category']; $where = " WHERE product.categoryid = $catid"; } 看起来您犯了一个小错误,因为在“默认”查询中,您已经有了WHERE语句,因此在if中,您应该再次使用和或或而不是WHERE: $where =

我想显示从表中选择的查询

<?php 
    $where = "";

    if(isset($_GET['category']))
    {
        $catid = $_GET['category'];
        $where = " WHERE product.categoryid = $catid";
    }

看起来您犯了一个小错误,因为在“默认”查询中,您已经有了
WHERE
语句,因此在if中,您应该再次使用
而不是
WHERE

$where = "";
if(isset($_GET['category']))
{
    $catid=$_GET['category'];
    $where = " AND product.categoryid = $catid";
}
此外,通过使用函数,您可以使附加的
条件的注入更加优雅。请看一个例子:

$additional_condition = 'AND active=1'
$statement = 'SELECT * FROM users WHERE condition=1 %s';
echo sprintf($statement, $additional_condition);

// Output: SELECT * FROM users WHERE condition=1 AND active=1

看起来您犯了一个小错误,因为在“默认”查询中,您已经有了
WHERE
语句,因此在if中,您应该再次使用
而不是
WHERE

$where = "";
if(isset($_GET['category']))
{
    $catid=$_GET['category'];
    $where = " AND product.categoryid = $catid";
}
此外,通过使用函数,您可以使附加的
条件的注入更加优雅。请看一个例子:

$additional_condition = 'AND active=1'
$statement = 'SELECT * FROM users WHERE condition=1 %s';
echo sprintf($statement, $additional_condition);

// Output: SELECT * FROM users WHERE condition=1 AND active=1