[英]result of prepared select statement as array
我想獲得一個准備好的語句作為數組(鍵/值對)的完整結果,以便以后在str_replace()
函數中使用它。
我的表具有三列,一個索引以及字段“ x1”和“ x2”。 我成功使用了以下內容:
$db = new mysqli("servername", "username", "pw", "dbname");
if($ps1 = $db->prepare("SELECT x1, x2 FROM my_table")) {
$ps1->execute();
$ps1->bind_result($search, $replace);
$result = array();
while ($ps1->fetch()) {
$result[$search] = $replace;
}
$ps1->close();
}
但是,我認為必須有一種更簡單的方法,沒有while
循環,才能獲得完整的結果,而不是從單行中一個接一個地累加。
我查看了其他問題,並提出了以下建議,但它不起作用(“警告:mysqli_fetch_assoc()期望參數1為mysqli_result”):
if($ps1 = $db->prepare("SELECT x1, x2 FROM my_table")) {
$ps1->execute();
$result = mysqli_fetch_assoc($ps1);
return $result;
$ps1->close();
}
我也嘗試了$result = mysqli_fetch_all($ps1);
沒有成功(獲取“調用未定義的函數mysqli_fetch_all()”)。
順便說一句,我正在使用PHP 5.6。
在有關MYSQLND的評論中的一些答案和討論之后的補充:
phpinfo()
在其mysqlnd
部分顯示以下信息:
加載的插件:mysqlnd,debug_trace,auth_plugin_mysql_native_password,auth_plugin_mysql_clear_password,auth_plugin_sha256_password
確實有一種更簡單的方法。 請考慮使用array_column :
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT x1, x2 FROM my_table";
if ($stmt = mysqli_prepare($link, $query)) {
/* execute statement */
mysqli_stmt_execute($stmt);
/* get result object */
$rows = mysqli_fetch_all(mysqli_stmt_get_result($stmt), MYSQLI_ASSOC);
/* get formatted object */
$result = array_column($rows, 'x2', 'x1');
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
編輯:更新的答案以使用程序化mysqli函數
這就是我使用mysqli用預備語句fetch_all
結果的方式
$stmt = $db->prepare("SELECT x1, x2 FROM my_table")
$stmt->execute();
$result = $stmt->get_result();
$allRows = $result->fetch_all(MYSQLI_ASSOC);
打擾一下我的英語
我認為這是不可能的,因為mysqli_fetch_assoc()
將mysqli_result
作為參數,而mysqli->prepare->execute
返回mysqli_stmt
對象。
您可以做的是使用JesusTheHun的答案的程序方式,也可以使用$ps1 = $db->query($stmt)
代替prepare and execute
,然后將其傳遞給$ps1->fetch_all(MYSQLI_ASSOC)
例:
if($ps1 = $db->query("SELECT x1, x2 FROM my_table")) {
$result = $ps1->fetch_all(MYSQLI_ASSOC);
$ps1->close();
return $result;
}
print_r($result);
mysqli的 PHP文檔
你嘗試過這樣的事情嗎?
$db = new mysqli("servername", "username", "pw", "dbname");
if($ps1 = $db->prepare("SELECT x1, x2 FROM my_table")) {
$ps1->execute();
$result = $ps1->fetchAll(PDO::FETCH_NAMED);
$ps1->close();
}
我的意思是這樣(如果您已經安裝了mysqlnd驅動程序)
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";
if ($stmt = mysqli_prepare($link, $query)) {
/* execute statement */
mysqli_stmt_execute($stmt);
/* get result object */
$result = mysqli_fetch_all(mysqli_stmt_get_result($stmt));
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
?>
您可以嘗試使用以下函數擴展mysqli_result類:
public function my_fetch_keyval() {
for ($result = array(); $tmp = $this->fetch_row();) {
// Expecting 2 columns. 1st = Key; 2nd = Value
$result[$tmp[0]] = $tmp[1];
}
return $result;
}
然后,您可以在代碼中使用它:
if($ps1 = $db->prepare("SELECT x1, x2 FROM my_table")) {
$ps1->execute();
$my_assoc_array = $ps1->my_fetch_keyval();
$ps1->close();
}
編輯:我經歷了您的評論和其他答案,很顯然您沒有Mysql本機驅動程序。
如果您不想使用mysqlnd,我相信您不能擺脫while循環;)
================================================== ==========
好像您錯過了一件事!
你需要調用get_result()
方法,然后fetch_all()
該對象上。
$db = new mysqli("servername", "username", "pw", "dbname");
if($ps1 = $db->prepare("SELECT x1, x2 FROM my_table")) {
$ps1->execute();
$result = $ps1->get_result()->fetch_all();
//return $result;
$ps1->close();
}
echo '<pre>';
print_r($result);
還要注意,我已經注釋掉return
語句,因為它在那一點終止了腳本的執行。
希望能幫助到你 :)
從前面的答案(缺少正確的mysqlnd
有兩個,一個用於mysqli
,一個用於PDO
),我們可以得出結論,您有兩個選擇,可以使用帶有while循環的准備好的語句或使用標准查詢,但以關聯數組的形式返回結果。
由於沒有任何參數,因此可以安全地使用普通ole查詢,因為:
a)沒有用戶必須轉義的輸入,語句中沒有參數,因此沒有安全風險
b)預處理語句的性能提升(如果有的話)是微不足道的,因為您將其用作普通的ole SQL查詢。 當您一次又一次地重復使用同一條語句時,僅更改綁定到該參數的參數值時,預准備的語句就會大放異彩
因此,讓我們開始追逐:
$conn = mysqli($host, $user, $pass, $dbname);
$query = $conn->query("SELECT x1, x2 FROM your_table");
$result = $query->fetch_assoc();
結果將采用以下格式:
[
'x1' => 'v1',
'x2' => 'v2'
]
如果您需要整個數據集而不僅僅是一個項目,請使用以下命令:
$result = $query->fetch_all(MYSQLI_ASSOC);
結果將如下所示:
[
[
'x1' => 'v1',
'x2' => 'v2'
],
[
'x1' => 'v1',
'x2' => 'v2'
],
...
]
更新
現在,我看到mysqli::fetch_all
也為您引發了一個錯誤,那么恐怕沒有循環就無法獲取整個數據集。 如果您想要替代方法,則可能是這樣的:
$query = $conn->query("SELECT x1, x2 FROM your_table");
$results = [];
for ($i = 0; $i < $query->num_rows; $i++) {
$row = $query->fetch_assoc();
$results[$row['x1']] = $row['x2'];
}
或這個:
while($row = $query->fetch_assoc()) {
$results[$row['x1']] = $row['x2'];
}
但老實說,那么您最好還是采用最初的方法,如果您沒有mysqli::fetch_all
可用的話,這是最好的方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.