【发布时间】:2014-03-20 18:07:48
【问题描述】:
所以正如标题所说,我在这里搜索并尝试了几乎所有方法,但没有成功。
在更深入之前,我尝试测试一些非常简单的东西,以确保它有效。但即使是最简单的,我总是得到 0 个结果,我知道有 67 个结果。
我的代码有什么问题?
谢谢
$conn = connect(); // connect to the db
$a_bind_params = array('love', 'circle');
$a_param_type = array('s', 's');
$totalKeywords = count($a_bind_params);
$q = 'SELECT id, name
FROM album
WHERE name LIKE ?';
for ($i = 1; $i < $totalKeywords; $i++) {
$q .= ' AND name LIKE ?';
}
echo $q; // for testing purposes: verify that query is OK
// bind parameters.
$param_type = '';
$n = count($a_param_type);
for($i = 0; $i < $n; $i++) {
$param_type .= $a_param_type[$i];
}
/* with call_user_func_array, array params must be passed by reference */
$a_params = array();
$a_params[] = & $param_type;
for($i = 0; $i < $n; $i++) {
/* with call_user_func_array, array params must be passed by reference */
$a_params[] = & $a_bind_params[$i];
}
$stmt = $conn->prepare($q);
/* use call_user_func_array, as $stmt->bind_param('s', $param); does not accept params array */
call_user_func_array(array($stmt, 'bind_param'), $a_params);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
echo $num_rows; // how many found ?
$stmt->bind_result($id, $name);
while($stmt->fetch()) {
echo $name;
}
$stmt->free_result();
$stmt->close();
$conn->close();
【问题讨论】:
-
正在寻找所谓的“重复”的答案,但仍然无法弄清楚
-
抱歉。我撤回了命名重复问题的尝试。这个问题与绑定参数无关,我应该更仔细地阅读它。不幸的是,我无法撤消我的近距离投票。
标签: php mysqli prepared-statement