mysqli where子句与php获取错误

时间:2016-07-01 00:01:09

标签: php html mysql variables sql-like

我试图在php中使用mysqli选择where子句和变量,但是我得到了这个错误

  

错误:我们的查询无法执行,原因如下:查询:SELECT * FROM产品WHERE'CroductCategory'LIKE笔记本电脑Errno:1054错误:'where子句'中的未知列'laptops'

  viewby.php
 <?php
 include('db/functions.php');
  $result= getproductsbycategory($_GET['ctg']);

while($product = $result->fetchassoc()){
    echo $product['ProductName'];
}

?>

//functions.php

 <?php
 function getproductsbycategory($ctg){
 include('connect.php');
     $sql = "SELECT * FROM Products WHERE 'ProductCategory' LIKE $ctg ";
 if(! $result = $mysqli->query($sql))
{
    echo "Error: Our query failed to execute and here is why: \n";
    echo "Query: " . $sql . "\n";
    echo "Errno: " . $mysqli->errno . "\n";
    echo "Error: " . $mysqli->error . "\n";
    exit;
}
return $result;
 }
  ?>
 //index.php

<a href="viewby.php?ctg=<?php echo $ctg; ?>">

 //viewby.php

<?php
 include('db/functions.php');
  $result= getproductsbycategory($_GET['ctg']);

while($product = $result->fetchassoc()){
    echo $product['ProductName'];
}

?>

我真的不知道是什么问题,如果我从phpmyadmin运行查询它工作正常,对不起,如果问题太明显 感谢

1 个答案:

答案 0 :(得分:0)

尝试使用此

更改您的查询
 $sql = "SELECT * FROM Products WHERE `ProductCategory` LIKE '%$ctg%' ";
你正在插入简单的引号&#39;而不是这个`