PHP + MySQL : Return function only show last result

标签 php mysql return

我正在为电报机器人编辑文件 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/

相关文章:

php - 如何在mysql中减去2个查询?

php - MySQL 仅选择过去 10 天的时间戳

c# - 在 C# 中查看返回值

javascript - 使用 TypeScript 函数返回 JSON 对象

javascript - 在 javascript 中显示从(00秒)开始的每个页面的计时器

php - Blade 模板引擎可以与codeigniter一起使用吗?

php - 使用 Ajax 数据逗号分隔的 SQL 查询来搜索表

php - 根据首页上的选择显示页面(包含图表)

PHPMyAdmin Designer 不显示关系

c - 将 -1 返回给用户定义的函数会导致程序终止,退出代码为 0