Php 准备语句中的错误-未定义变量:sql和致命错误:调用成员函数bind_param()

Php 准备语句中的错误-未定义变量:sql和致命错误:调用成员函数bind_param(),php,sql,Php,Sql,我的代码中有两个错误: Notice: Undefined variable: sql AND Fatal error: Call to a member function bind_param() on a non-object 我的代码是: //if there is a plant name if (isset($_POST['plant_name']) && $_POST['plant_name']) { $where .= "AND (common_name) LI

我的代码中有两个错误:

Notice: Undefined variable: sql
AND
Fatal error: Call to a member function bind_param() on a non-object
我的代码是:

//if there is a plant name
if (isset($_POST['plant_name']) && $_POST['plant_name']) { 
$where .= "AND (common_name) LIKE ? OR (latin_name) LIKE ?";
}
$stmt = $conn2->prepare($sql . $where);
if (isset($_POST['plant_name']) && $_POST['plant_name']) { 
$stmt->bind_param('s', strtolower($_POST['plant_name']));
$stmt->bind_param('s', strtolower($_POST['plant_name'])."%");
}

//execute query
$stmt->execute();

// get the roses and do the query!
$sql = "SELECT * FROM rosename";

//do we have a 'where string' to add to this query
if ($where) {
$query .= $where;
}

$sql = mysql_query($query, $conn2);
我基本上是想让某人在plant_name字段中键入一个plant,然后查看它是否像DB中的latin_name和common_name属性中的任何值一样


有人能帮我一下吗。

由于准备的查询是错误的,因此,
$stmt
对象可能被设置为false

试试这个:

$stmt = $conn2->prepare($sql . $where);

if (false === $stmt) {
    var_dump($conn2->errorCode();
}
您可以通过PHP文档阅读


此外,还需要将$sql和$where初始化移到$stmt代码块上方。这就是为什么会出现未定义的错误。

您似乎在使用未定义的变量$sql准备语句,这就是通知错误的意思。在您的代码中,我们看不到在您尝试使用该变量的prepare语句之前定义的名为$sql的任何变量

$sql=“SELECT*..
。我想这应该是
$query=“SELECT*..
。在
前面加一个空格,这样你就不会得到