1 回答
TA贡献1860条经验 获得超9个赞
您正在为查询中的每个结果发送一条消息,因为 while 循环是针对所有行执行的。如果我理解正确的话,您想发送一条包含所有查询结果的消息。我建议您使用PDO连接数据库并从数据库获取数据/向数据库插入数据,因为这样更安全。
尝试这个:
<?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']);
}
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报