在帶有數組的 IN 語句之後,在 WHERE 語句中使用更多佔位符 (Use more placeholders in WHERE statement after IN statement with arrays)


問題描述

在帶有數組的 IN 語句之後,在 WHERE 語句中使用更多佔位符 (Use more placeholders in WHERE statement after IN statement with arrays)

我需要將更多佔位符添加到以下查詢中。這可能嗎?我不知道從哪裡開始,我在 Internet 上找不到任何選項。

現在查詢:

$in2  = str_repeat('?,', count($arrayid) ‑ 1) . '?';
$sql2 = "SELECT COUNT(id) AS totalacc FROM account WHERE id IN ($in2) ";
$stmt2  = $mysqli‑>prepare($sql2);
$types2 = str_repeat('i', count($arrayid));
$stmt2‑>bind_param($types2,...$arrayid);
$stmt2‑>execute();
$stmt2‑>bind_result($row['totalacc']);
    while($stmt2‑>fetch()) $totalacc = $row['totalacc']; 

我的目標查詢:

$countname1 = '(Hallo)';
    $countname = trim(filter_var("%{$countname1}%", FILTER_SANITIZE_STRING));
$in2  = str_repeat('?,', count($arrayid) ‑ 1) . '?';
$sql2 = "SELECT COUNT(id) AS totalacc FROM account WHERE id IN ($in2) AND name LIKE ?";
$stmt2  = $mysqli‑>prepare($sql2);
$types2 = str_repeat('i', count($arrayid));
$stmt2‑>bind_param($types2,s,...$arrayid,$countname); // Will never work, but how to do this? 
$stmt2‑>execute();
$stmt2‑>bind_result($row['totalacc']);
    while($stmt2‑>fetch()) $totalacc = $row['totalacc']; 

參考解法

方法 1:

You just need to append the extra values to your array of parameters and string of types. This should work (untested):

$countname1 = '(Hallo)';
$countname = trim(filter_var("%{$countname1}%", FILTER_SANITIZE_STRING));
$in2  = str_repeat('?,', count($arrayid) ‑ 1) . '?';

$sql2 = "SELECT COUNT(id) AS totalacc FROM account WHERE id IN ($in2) AND name LIKE ?";
$stmt2  = $mysqli‑>prepare($sql2);

$types2 = str_repeat('i', count($arrayid));
$types2 .= "s"; //append "s" to the end of the $types2 string.
$arrayid[] = $countname; //append the value of $countname to the array of parameters

$stmt2‑>bind_param($types2, ...$arrayid); 
$stmt2‑>execute();
//...etc

方法 2:

The easiest solution would be to use PDO instead of mysqli. This would be so much easier.

If you are stuck with mysqli then you can achieve a similar thing by simply ignoring the types and appending your result to the array.

$countname1 = '(Hallo)';
$countname = "%{$countname1}%";
$in2 = str_repeat('?,', count($arrayid) ‑ 1) . '?';
$sql2 = "SELECT COUNT(id) AS totalacc FROM account WHERE id IN ($in2) AND name LIKE ?";
$stmt2 = $mysqli‑>prepare($sql2);

$arrayid[] = $countname;
$stmt2‑>bind_param(str_repeat('s', count($arrayid)), ...$arrayid);
$stmt2‑>execute();
$stmt2‑>bind_result($totalacc);
$stmt2‑>fetch();

You could even write a function to abstract from all of this code.

(by user15259803ADysonDharman)

參考文件

  1. Use more placeholders in WHERE statement after IN statement with arrays (CC BY‑SA 2.5/3.0/4.0)

#mysqli #PHP #prepared-statement






相關問題

如何第一次創建 mysqli 數據庫? (how can I creates a mysqli database for first the first time?)

使用 jquery autosuggest 從多個表中選擇 (select from multiple tables with jquery autosuggest)

無法連接 - 數據庫或編碼錯誤 (Could Not Connect - Database or Coding Error)

mysqli忽略表中的第一行 (mysqli ignoring the first row in a table)

從數據庫導入的鏈接以 UTF8 截斷 (imported link from database cut off at UTF8)

試圖獲取非對象的屬性 (Trying to get property of non-object)

獲取 php 和 mysqli 中插入數據的 ID (Get Id of inserted data in php and mysqli)

WAMP 和 mysqli::real_connect(): (HY000/2002)? (WAMP and mysqli::real_connect(): (HY000/2002)?)

如何將 MySQLi 準備好的語句的結果放入關聯數組中? (How can I put the results of a MySQLi prepared statement into an associative array?)

測試選擇查詢是否找到結果 (Testing if a Select Query found results)

為什麼我的正確連接嘗試在 apache2 下失敗但在 cli 下失敗? (Why is my correct connection attempt failing under apache2 but not under cli?)

在帶有數組的 IN 語句之後,在 WHERE 語句中使用更多佔位符 (Use more placeholders in WHERE statement after IN statement with arrays)







留言討論