Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP+;Mysql:返回函数只显示最后一个结果_Php_Mysql_Return - Fatal编程技术网

PHP+;Mysql:返回函数只显示最后一个结果

PHP+;Mysql:返回函数只显示最后一个结果,php,mysql,return,Php,Mysql,Return,我正在编辑电报机器人的php文件。当我在telegram上测试时,它只显示代码的最后一个值(但在php命令行上,有多个值): if(mysqli\u num\u行($cari)>0){ while($row=mysqli\u fetch\u数组($cari)){ $hasil=“QS.[”$row[“surat”]。“-”$row[“nosurat”]。”]:“$row[“ayat”]。”:“$row[“ayattext”]。”; var_dump($hasil); } }否则{ $hasil

我正在编辑电报机器人的php文件。当我在telegram上测试时,它只显示代码的最后一个值(但在php命令行上,有多个值):

if(mysqli\u num\u行($cari)>0){
while($row=mysqli\u fetch\u数组($cari)){
$hasil=“QS.[”$row[“surat”]。“-”$row[“nosurat”]。”]:“$row[“ayat”]。”:“$row[“ayattext”]。”;
var_dump($hasil);
} 
}否则{
$hasil=“0结果”;
}
返回$hasil;
mysqli_close($conn);
当我更改函数时,创建对该函数的响应:

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

?>

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

<?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;
}

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

<?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;
}

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

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

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

$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命令行中显示此错误:警告:file\u get\u contents():无法打开流:HTTP请求失败!HTTP/1.1 400第39行G:\xampp\htdocs\xbot\file.php中的错误请求,这是第39行中的内容:$result=file\u get\u contents(请求url('sendMessage'),false,$context);很可能你又犯了一个错误。但我看不出与上述代码有任何联系。该代码行肯定不是您提出问题的函数的一部分。很抱歉,无法说明您未在此处发布的代码。Arghh太长,无法在此处发布。请仅发布经过清理、缩短的代码,足以演示您的问题。永远不要发布你所有的代码。并始终在问题本身中包含所有附加信息(有一个
编辑
按钮),不要在评论中发布附加信息。已经尝试过这种方法,但我会按照您的建议重试。但根本没有结果,在php命令行中显示此错误:警告:file\u get\u contents():无法打开流:HTTP请求失败!HTTP/1.1 400第39行G:\xampp\htdocs\xbot\file.php中的错误请求,这是第39行中的内容:$result=file\u get\u contents(请求url('sendMessage'),false,$context);很可能你又犯了一个错误。但我看不出与上述代码有任何联系。该代码行肯定不是您提出问题的函数的一部分。很抱歉,无法说明您未在此处发布的代码。Arghh太长,无法在此处发布。请仅发布经过清理、缩短的代码,足以演示您的问题。永远不要发布你所有的代码。并始终在问题本身中包含所有附加信息(有一个
编辑
按钮),不要在评论中发布附加信息。已经尝试过这种方法,但我会按照您的建议重试。但根本没有结果,在php命令行中显示此错误:警告:file\u get\u contents(api.telegram.org/bottoken/sendMessage):无法打开流:HTTP请求失败!HTTP/1.1 400第39行G:\xampp\htdocs\xbot\file.php中的错误请求,这是第39行中的内容:$result=file\u get\u contents(请求url('sendMessage'),false,$context);你上面描述的问题与你问题中描述的问题无关。在这种情况下,我建议你打开另一个问题并解释另一个问题。当然,有人会帮你的。这个问题已经由我和@arkascha解决了。谢谢Marcos和@arkascha。我会编辑并发布完整的代码。已经尝试过这种方式,但我会按照您的建议重试。但根本没有结果,在php命令行中显示此错误:警告:file\u get\u contents(api.telegram.org/bottoken/sendMessage):无法打开流:HTTP请求失败!HTTP/1.1 400第39行G:\xampp\htdocs\xbot\file.php中的错误请求,这是第39行中的内容:$result=file\u get\u contents(请求url('sendMessage'),false,$context);你上面描述的问题与你问题中描述的问题无关。在这种情况下,我建议你打开另一个问题并解释另一个问题。当然,有人会帮你的。这个问题已经由我和@arkascha解决了。谢谢Marcos和@arkascha。我会编辑并发布完整的代码。