【问题标题】:Iterating through two columns of a MySQL table遍历 MySQL 表的两列
【发布时间】:2021-12-05 18:33:12
【问题描述】:

我正在用 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].")",
]);
}

【问题讨论】:

  • SELECT id, name FROM user

标签: php mysql mysqli telegram


【解决方案1】:

您可以在单个查询中选择多个列。

另外,不要多次执行查询。您可以使用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'].")",
    ]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-08
    • 2011-06-24
    相关资源
    最近更新 更多