duanpu1064 2015-10-19 17:06
浏览 91
已采纳

为什么会出现这个PHP错误:“严格的标准:mysqli :: next_result():没有下一个结果集。”?

I have code, which is basically a copy of a php.net's code, but for some reason it does not work. Here is the code on php.net:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
}

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s
", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------
");
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

The first change I made was the connection:

$mysqli = new mysqli("localhost", "root", "", "fanfiction");

The second change I made was the queries:

$query = "SELECT first FROM tests;";
$query .= "SELECT second FROM tests;";
$query .= "SELECT third FROM tests;";
$query .= "SELECT fourth FROM tests";

EDIT: The full code with my changes

<?php
$mysqli = new mysqli("localhost", "root", "", "fanfiction");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
}

$query = "SELECT first FROM tests;";
$query .= "SELECT second FROM tests;";
$query .= "SELECT third FROM tests;";
$query .= "SELECT fourth FROM tests";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s
", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------
");
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

The error I get:

Strict standards: mysqli::next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method in address on line line number

I searched a solution over the net, and particularly here on StackOverflow, but I did not find helpful solutions. Most of the solutions I found were one of those two:

  • In this solution,@Hammerite says to change the loop from do-while to while. This suggest that php.net's code has a problem in its logic, and I find it very hard to believe. But more importantly, it just does not work for me.
  • In this solution, @mickmackusa suggests to add a condition in the while and change $mysqli->next_result() to $mysqli->next_result() && $mysqli->more_results(), but this solution do not work quite well. It does indeed removes the error but it omits the last result.
  • 写回答

1条回答 默认 最新

  • duan00529 2015-10-19 17:46
    关注

    Try it with

    } while ($mysqli->more_results() && $mysqli->next_result());
    

    sscce:

    <?php
    ini_set('display_errors', 'on');
    error_reporting(E_ALL|E_STRICT);
    
    $mysqli = new mysqli("localhost", "localonly", "localonly", "test");
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s
    ", mysqli_connect_error());
        exit();
    }
    
    $mysqli->query('CREATE TEMPORARY TABLE City (ID int auto_increment, `Name` varchar(32), primary key(ID))') or die($mysqli->error);
    
    $stmt = $mysqli->prepare("INSERT INTO City (`Name`) VALUES (?)") or die($mysqli->error);
    $stmt->bind_param('s', $city) or die($stmt->error);
    foreach(range('A','Z') as $c) {
        $city = 'city'.$c;
        $stmt->execute() or die($stmt->error);
    }
    
    $query  = "SELECT CURRENT_USER();";
    $query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
    
    /* execute multi query */
    if (!$mysqli->multi_query($query)) {
        trigger_error('multi_query failed: '.$mysqli->error, E_USER_ERROR);
    }
    else {
        do {
            /* store first result set */
            if ($result = $mysqli->store_result()) {
                while ($row = $result->fetch_row()) {
                    printf("'%s'
    ", $row[0]);
                }
                $result->free();
            }
            /* print divider */
            if ($mysqli->more_results()) {
                printf("-----------------
    ");
            }
        } while ($mysqli->more_results() && $mysqli->next_result());
    }
    

    prints

    'localonly@localhost'
    -----------------
    'cityU'
    'cityV'
    'cityW'
    'cityX'
    'cityY'
    

    without warnings/notices.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作