我正在为电报机器人编辑文件 php。当我在电报上测试时,它只显示此代码的最后一个值(但在 php 命令行上,有多个值):
if (mysqli_num_rows($cari) > 0) {
while($row = mysqli_fetch_array($cari)) {
$hasil = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" . $row["ayat"]. ": " . $row["ayattext"]. ". ";
var_dump($hasil);
}
} else {
$hasil = "0 results";
}
return $hasil;
mysqli_close($conn);
当我将函数 create_response 更改为:
if (mysqli_num_rows($cari) > 0) {
while($row = mysqli_fetch_array($cari)) {
$hasil = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" . $row["ayat"]. ": " . $row["ayattext"]. ". ";
var_dump($hasil);
}
} else {
$hasil = "0 results";
}
return $hasil;
mysqli_close($conn);
它有效,但它只显示电报上的最后一个值。但是在 php 命令行上显示我想要的完整结果。
如何解决这个问题?在此之前感谢...
完整代码如下:
<?php
include("token.php");
//include("db.php");
function request_url($method)
{
global $TOKEN;
return "https://api.telegram.org/bot" . $TOKEN . "/". $method;
}
function get_updates($offset)
{
$url = request_url("getUpdates")."?offset=".$offset;
$resp = file_get_contents($url);
$result = json_decode($resp, true);
if ($result["ok"]==1)
return $result["result"];
return array();
}
function send_reply($chatid, $msgid, $text)
{
$data = array(
'chat_id' => $chatid,
'text' => $text,
'reply_to_message_id' => $msgid
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents(request_url('sendMessage'), false, $context);
print_r($result);
}
function create_response($text)
{
$conn = mysqli_connect("localhost","root","xxx","aq");
$data = array();
$sql = "Select s.text_sr AS surat, s.no_sr AS nosurat, qi.verseid AS ayat, qi.ayahtext AS ayattext from quranindonesia qi left join surah s on qi.suraid=s.no_sr where qi.ayahtext like '%$text%' limit 3,5";
$cari = mysqli_query($conn, $sql);
//$hasil = '';
if (mysqli_num_rows($cari) > 0) {
$hasil = array();
// output data of each row
while($row = mysqli_fetch_array($cari)) {
$hasil[] = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" . $row["ayat"]. ": " . $row["ayattext"]. ". ";
//var_dump($hasil);
}
} else {
$hasil = "0 results";
}
return $hasil;
mysqli_close($conn);
}
function process_message($message)
{
$updateid = $message["update_id"];
$message_data = $message["message"];
if (isset($message_data["text"])) {
$chatid = $message_data["chat"]["id"];
$message_id = $message_data["message_id"];
$text = $message_data["text"];
$response = create_response($text);
send_reply($chatid, $message_id, $response);
}
return $updateid;
}
function process_one()
{
$update_id = 0;
if (file_exists("last_update_id")) {
$update_id = (int)file_get_contents("last_update_id");
}
$updates = get_updates($update_id);
foreach ($updates as $message)
{
$update_id = process_message($message);
}
file_put_contents("last_update_id", $update_id + 1);
}
while (true) {
process_one();
}
?>
最佳答案
你可以像这样使用一个数组:
$linesArr = array();
if (mysqli_num_rows($cari) > 0) {
while($row = mysqli_fetch_array($cari)) {
$linesArr[] = "QS.[".$row["surat"]."-" . $row["nosurat"]. "]:" . $row["ayat"]. ": " . $row["ayattext"];
}
$hasil = implode(", ", $linesArr) . ".";
} else {
$hasil = "0 results";
}
return $hasil;
mysqli_close($conn);
关于PHP + MySQL : Return function only show last result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31493684/