[英]iterating through two columns of a mysql table with php
I'm making a bot in PHP for telegram API and I'm using MYSQL.我正在用 PHP 为电报 API 制作一个机器人,我正在使用 MYSQL。 I have a table for every user that stores id, name, etc. I wrote a piece of code to send number of users and select the id of every user to make a link with it, using a loop.我有一个用于存储 id、name 等的每个用户的表。我编写了一段代码来发送用户数量并选择每个用户的 id 以使用循环与其建立链接。 I was wondering how I can iterate through the names at the same time do I need another loop or I should use MYSQL code ?我想知道如何同时遍历名称我需要另一个循环还是应该使用 MYSQL 代码? thanks in advance.提前致谢。
$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].")",
]);
}
you can select multiple columns in a single query.您可以在单个查询中选择多个列。
Also, don't execute the query multiple times.另外,不要多次执行查询。 You can use mysqli_num_rows($idarray)
to get the number of rows.您可以使用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'].")",
]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.