我正在用 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/yidong/324279.html
