亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Telegram Bot 和 Mysqli_fetch_assoc 問題

Telegram Bot 和 Mysqli_fetch_assoc 問題

PHP
MM們 2023-08-11 17:46:05
我正在用 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']);  }}
查看完整描述

1 回答

?
慕碼人2483693

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']);

}


查看完整回答
反對 回復 2023-08-11
  • 1 回答
  • 0 關注
  • 128 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號