请查看PHP代码:
$val = $_POST['name'];
$stmt = $mysqli->stmt_init();
$stmt->prepare("SELECT id, name, email FROM some_table WHERE name LIKE ?%");
$stmt->bind_param('s', $val);
$stmt->execute();
当我运行此查询时;我收到以下错误: 警告:mysqli_stmt :: bind_param():无效的对象或资源mysqli_stmt
这里有什么问题?
答案 0 :(得分:0)
问题是?
应该作为占位符保留在SQL表达式中,或者 - 再次作为占位符,在SQL函数中使用。所以这里有两个选项:只需将%
添加到提供的值,如下所示:
$val = $_POST['name'] . '%';
$stmt->prepare("SELECT id, name, email FROM some_table
WHERE name LIKE ?");
$stmt->bind_param('s', $val);
...或保留该值,改为修改SQL:
$stmt->prepare("SELECT id, name, email FROM some_table
WHERE name LIKE CONCAT(?, '%')");
$stmt->bind_param('s', $val);
答案 1 :(得分:0)
将其更改如下:
$val = $_POST['name'] . '%';
$stmt = $mysqli->stmt_init();
$stmt->prepare("SELECT id, name, email FROM some_table WHERE name LIKE ?");
$stmt->bind_param('s', $val);
$stmt->execute();
基本上,准备好的stmt应该只有占位符'?',而你的变量应该在末尾有通配符'%'