我的代码中出现2个错误:
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字段中键入一个工厂,然后查看它是否与数据库中latin_name和common_name属性的任何值相似。
请有人帮帮我。
答案 0 :(得分:2)
$stmt
对象可能设置为false,因为正在准备的查询是错误的。
试试这个:
$stmt = $conn2->prepare($sql . $where);
if (false === $stmt) {
var_dump($conn2->errorCode();
}
您可以通过PHP文档阅读error code和prepare。
另外,您需要将$ sql和$初始化移动到$ stmt代码块之上。这就是你得到未定义错误的原因。
答案 1 :(得分:0)
您似乎正在使用未定义的变量$ sql准备语句,这就是通知错误所说的内容。在您的代码中,我们无法在您尝试使用该变量的prepare语句之前看到任何名为$ sql的变量。