Mysqli - 将结果绑定到数组 [英] Mysqli - Bind results to an Array
问题描述
我最近从 mysql 切换到 mysqli 并开始使用准备好的语句.在mysql中我们做
I recently switched to mysqli from mysql and started using prepared statements. in mysql we do
$result = mysql_query("SELECT * FROM table WHERE id = ?");
所以我们在一个变量中得到了整个表的数组.
So we get the array of whole table in one variable.
但在 mysqli 中我们这样做
But in mysqli we do
mysqli_stmt_bind_result($stmt, $result);
所以基本上这里只有一个变量被绑定到变量结果.我们如何获得从 mysql 获得的相同变量(数组)?
PS - 希望我的问题足够清楚.我知道他们的方法并不多,但我很喜欢最好的方法.
PSS - 我更喜欢程序化的方式.
so basically here only one variable gets bind to variable result.
How can we get the same variable(array) that we got from mysql?
PS - Hope my ques is clear enough. I know their are not many methods possible but i am luking for the best one.
PSS - I am more comfortable with the procedural way.
推荐答案
最后这段代码可以工作了!!!!!!
Finally this code is Working !!!!!
<?php
$host = 'localhost';
$user = 'root';
$pass = '1234';
$data = 'test';
$mysqli = new mysqli($host, $user, $pass, $data);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($stmt = $mysqli->prepare("SELECT * FROM sample WHERE t2 LIKE ?")) {
$tt2 = '%';
$stmt->bind_param("s", $tt2);
$stmt->execute();
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field())
{
$params[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);
while ($stmt->fetch()) {
foreach($row as $key => $val)
{
$c[$key] = $val;
}
$result[] = $c;
}
$stmt->close();
}
$mysqli->close();
print_r($result);
?>
这篇关于Mysqli - 将结果绑定到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!