php - How to get array of row objects from my result in mysqli prepared query -
i return results prepared query (mysqli) objects in array cant find fetchall method or similar. how go this?
public function getimageresults ($search_term) { if (empty($search_term)) return false; $image_query = $this->db_connection->stmt_init(); $image_query_sql = " select images.url url images, imagesearch, imagesearchresults imagesearch.search_string = ? , imagesearchresults.search_id = imagesearch.id , imagesearchresults.image_id = images.id , images.deleted = 0 , imagesearch.deleted = 0 , 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 return entire result set here... ideas? } return false; }
i found code on net kinda works error when use
notice: use of undefined constant mysqli_stmt_bind_result - assumed 'mysqli_stmt_bind_result'
private function getresult ($stmt) { $result = array(); $metadata = $stmt->result_metadata(); $fields = $metadata->fetch_fields(); (;;) { $pointers = array(); $row = new stdclass(); $pointers[] = $stmt; foreach ($fields $field) { $fieldname = $field->name; $pointers[] = &$row->$fieldname; } call_user_func_array(mysqli_stmt_bind_result, $pointers); if (!$stmt->fetch()) break; $result[] = $row; } $metadata->free(); return $result; }
the mysqli_stmt_bind_result
on line:
call_user_func_array(mysqli_stmt_bind_result, $pointers);
should quoted:
call_user_func_array('mysqli_stmt_bind_result', $pointers);
you use pdo, untested example should work:
public function getimageresults ($search_term) { $sql = " select images.url url images, imagesearch, imagesearchresults imagesearch.search_string = ? , imagesearchresults.search_id = imagesearch.id , imagesearchresults.image_id = images.id , images.deleted = 0 , imagesearch.deleted = 0 , 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; }
Comments
Post a Comment