PHP + Mysql:返回函数只显示最后的结果

时间:2015-07-18 17:29:29

标签: 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();
}

?>

2 个答案:

答案 0 :(得分:0)

return语句总是返回一个参数。这就是它的工作原理。如果您需要所有行,则必须将它们放入数组并返回。这是一个简单的演示:

<?php
function returnRows($cari) {
    $hasil = [];
    while($row = mysqli_fetch_array($cari)) {
        $hasil[] = sprintf(
            "QS.[%s-%s]:%s: %s. ",
            $row["surat"], 
            $row["nosurat"], 
            $row["ayat"], 
            $row["ayattext"]
        );
    } 
    return $hasil;
}

这将返回一个数组,当你在赋值中连接它时,数组的每个元素都包含一个字符串。

答案 1 :(得分:0)

您可以使用数组,如下所示:

$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);