Telegram 机器人和 Mysqli_fetch_assoc 问题
Telegram Bot and Mysqli_fetch_assoc problem
我正在 PHP 制作 Telegram 机器人。我已经连接了一个 Mysql 数据库,我需要在一条独特的消息中打印一些结果。但是当我启动命令时,它会在一些消息中打印出每个结果。我怎样才能在一条独特的消息中打印所有结果?
这是代码:
if($data == 'rawliberi'){
$thequery = "SELECT * FROM uwgliberi WHERE roster='OCW'";
$thequeryup = mysqli_query($conn, $thequery);
while($row = mysqli_fetch_assoc($thequeryup)){
$Alex -> Request('sendMessage', ['chat_id' => $chat_id, 'text' => $row['wrestlername'], 'parse_mode' => 'HTML']);
}
}
您正在为查询中的每个结果发送一条消息,因为对所有行都执行了 while 循环。如果我理解正确,您想发送一条包含所有查询结果的消息。我建议你使用 PDO to connect and fetch/insert data from/to a database because is more secure.
试试这个:
<?php
$pdo = new PDO("mysql:host=localhost;dbname=DATABASE_NAME", "USERNAME", "PASSWORD");
if($data == 'rawliberi'){
$allResults = "";
$stmt = $pdo->prepare("SELECT * FROM uwgliberi WHERE roster=:roster"); // Prepare the query
$stmt->execute(['roster' => "OCW"]); // Replace parameters starting with ":" with the given value (e.g. replace ":roster" with "OCW") and execute the query
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC); // Insert results in the $results variable
/*
Result's value example
[
{
"id": "1",
"wrestlername": "Nicola",
"roster": "OCW"
},
{
"id": "2",
"wrestlername": "Mauro",
"roster": "OCW"
},
{
"id": "3",
"wrestlername": "Don Gino",
"roster": "OCW"
}
]
*/
for ($i = 0; $i < count($results); $i++){ // Execute this loop for each result in $results. The count() function return the count of the results
$allResults .= $results[$i]['wrestlername'] . "\n"; // Add the wrestlername to the allResults variable and go to a new line (\n) so the names won't be "NicolaMauroDon Gino"
}
$Alex->Request('sendMessage', ['chat_id' => $chat_id, 'text' => $allResults, 'parse_mode' => 'HTML']);
}
我正在 PHP 制作 Telegram 机器人。我已经连接了一个 Mysql 数据库,我需要在一条独特的消息中打印一些结果。但是当我启动命令时,它会在一些消息中打印出每个结果。我怎样才能在一条独特的消息中打印所有结果?
这是代码:
if($data == 'rawliberi'){
$thequery = "SELECT * FROM uwgliberi WHERE roster='OCW'";
$thequeryup = mysqli_query($conn, $thequery);
while($row = mysqli_fetch_assoc($thequeryup)){
$Alex -> Request('sendMessage', ['chat_id' => $chat_id, 'text' => $row['wrestlername'], 'parse_mode' => 'HTML']);
}
}
您正在为查询中的每个结果发送一条消息,因为对所有行都执行了 while 循环。如果我理解正确,您想发送一条包含所有查询结果的消息。我建议你使用 PDO to connect and fetch/insert data from/to a database because is more secure.
试试这个:
<?php
$pdo = new PDO("mysql:host=localhost;dbname=DATABASE_NAME", "USERNAME", "PASSWORD");
if($data == 'rawliberi'){
$allResults = "";
$stmt = $pdo->prepare("SELECT * FROM uwgliberi WHERE roster=:roster"); // Prepare the query
$stmt->execute(['roster' => "OCW"]); // Replace parameters starting with ":" with the given value (e.g. replace ":roster" with "OCW") and execute the query
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC); // Insert results in the $results variable
/*
Result's value example
[
{
"id": "1",
"wrestlername": "Nicola",
"roster": "OCW"
},
{
"id": "2",
"wrestlername": "Mauro",
"roster": "OCW"
},
{
"id": "3",
"wrestlername": "Don Gino",
"roster": "OCW"
}
]
*/
for ($i = 0; $i < count($results); $i++){ // Execute this loop for each result in $results. The count() function return the count of the results
$allResults .= $results[$i]['wrestlername'] . "\n"; // Add the wrestlername to the allResults variable and go to a new line (\n) so the names won't be "NicolaMauroDon Gino"
}
$Alex->Request('sendMessage', ['chat_id' => $chat_id, 'text' => $allResults, 'parse_mode' => 'HTML']);
}