我正在用 PHP 為電報 API 制作一個機器人,我正在使用 MYSQL。我為每個用戶存盤了一個表,其中存盤了 id、name 等。我撰寫了一段代碼來發送用戶數并選擇每個用戶的 id 以使用回圈與其建立鏈接。我想知道如何同時遍歷名稱我需要另一個回圈還是應該使用 MYSQL 代碼?提前致謝。
$alltotal = mysqli_num_rows(mysqli_query($connect,"select id from user"));
$ids=array();
$idarray =mysqli_query($connect,"select id from user");
api('sendmessage',[
'chat_id'=>$chat_id,
'text'=>"total users : $alltotal: ",
]);
while($row= mysqli_fetch_array($idarray)){
$ids[]=$row['id'];
api('sendmessage',[
'parse_mode'=>'MarkdownV2',
'chat_id'=>$chat_id,
'text'=>"(tg://user?id=".$row[0].")",
]);
}
uj5u.com熱心網友回復:
您可以在單個查詢中選擇多個列。
另外,不要多次執行查詢。您可以使用mysqli_num_rows($idarray)來獲取行數。
$idarray = mysqli_query($connect,"select id, name from user");
$alltotal = mysqli_num_rows($idarray);
api('sendmessage',[
'chat_id'=>$chat_id,
'text'=>"total users : $alltotal: ",
]);
$ids_and_names = [];
while($row= mysqli_fetch_assoc($idarray)){
$ids_and_names[] = $row;
api('sendmessage',[
'parse_mode'=>'MarkdownV2',
'chat_id'=>$chat_id,
'text'=>"(tg://user?id=".$row['id'].")",
]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322916.html
上一篇:具有多種排序和集合的映射(例如,不是[]型別的映射索引)
下一篇:視窗函式查詢問題?
