Php 如何在mysqli准备好的查询中从我的结果中获取行对象数组
我想将准备好的查询(mysqli)的所有结果作为数组中的对象返回,但我找不到fetchall方法或类似的方法。我该怎么办Php 如何在mysqli准备好的查询中从我的结果中获取行对象数组,php,mysqli,prepared-statement,Php,Mysqli,Prepared Statement,我想将准备好的查询(mysqli)的所有结果作为数组中的对象返回,但我找不到fetchall方法或类似的方法。我该怎么办 public function getImageResults ($search_term) { if (empty($search_term)) return false; $image_query = $this->db_connection->stmt_init(); $image_
public function getImageResults ($search_term)
{
if (empty($search_term))
return false;
$image_query = $this->db_connection->stmt_init();
$image_query_sql =
"
SELECT
Images.url as url
FROM
Images, ImageSearch, ImageSearchResults
WHERE
ImageSearch.search_string = ? AND
ImageSearchResults.search_id = ImageSearch.id AND
ImageSearchResults.image_id = Images.id AND
Images.deleted = 0 AND
ImageSearch.deleted = 0 AND
ImageSearchResults.deleted = 0
";
if ($image_query->prepare($image_query_sql))
{
$image_query->bind_param('s', $search_term);
$image_query->execute();
$image_query->store_result();
$image_query->bind_result($url);
return //need to return the entire result set here... any ideas?
}
return false;
}
我在网上找到了这段代码,它可以正常工作,但我在使用它时遇到了这个错误
注意:未定义常量的使用
mysqli\u stmt\u bind\u结果-假设
'mysqli_stmt_bind_result'
这一行的
mysqli\u stmt\u bind\u结果
:
call_user_func_array(mysqli_stmt_bind_result, $pointers);
应引用:
call_user_func_array('mysqli_stmt_bind_result', $pointers);
您也可以使用一个未经测试的示例,但它应该可以工作:
public function getImageResults ($search_term)
{
$sql =
"
SELECT
Images.url as url
FROM
Images, ImageSearch, ImageSearchResults
WHERE
ImageSearch.search_string = ? AND
ImageSearchResults.search_id = ImageSearch.id AND
ImageSearchResults.image_id = Images.id AND
Images.deleted = 0 AND
ImageSearch.deleted = 0 AND
ImageSearchResults.deleted = 0
";
$pdo = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'username', 'password');
$sth = $pdo->prepare($sql);
$sth->execute(array($search_term));
$result = $sth->fetchAll(PDO::FETCH_CLASS, "ArrayObject");
//var_dump($result);//debug
return $result;
}
这一行的
mysqli\u stmt\u bind\u结果
:
call_user_func_array(mysqli_stmt_bind_result, $pointers);
应引用:
call_user_func_array('mysqli_stmt_bind_result', $pointers);
您也可以使用一个未经测试的示例,但它应该可以工作:
public function getImageResults ($search_term)
{
$sql =
"
SELECT
Images.url as url
FROM
Images, ImageSearch, ImageSearchResults
WHERE
ImageSearch.search_string = ? AND
ImageSearchResults.search_id = ImageSearch.id AND
ImageSearchResults.image_id = Images.id AND
Images.deleted = 0 AND
ImageSearch.deleted = 0 AND
ImageSearchResults.deleted = 0
";
$pdo = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'username', 'password');
$sth = $pdo->prepare($sql);
$sth->execute(array($search_term));
$result = $sth->fetchAll(PDO::FETCH_CLASS, "ArrayObject");
//var_dump($result);//debug
return $result;
}
嗯,如果我没记错的话,你可以试试这样:
call_user_func_array(array($stmt, 'bind_result'), $pointers);
嗯,如果我没记错的话,你可以试试这样:
call_user_func_array(array($stmt, 'bind_result'), $pointers);
酷thx:)。。这是得到我想要的东西的最好方法吗。这是我第一次使用mysqli和prepared语句,我是否应该改用其他语句-我只使用mysqli来转义使用prepared语句的输出。使用PDO和ArrayObject examplecool thx更新了答案:)。。这是得到我想要的东西的最好方法吗。这是我第一次使用mysqli和prepared语句,我是否应该使用其他语句来代替它-我只使用mysqli来转义使用prepared语句的输出。使用PDO和ArrayObject示例更新了答案