首页 > 解决方案 > 如何从准备语句 PHP 中收集结果?

问题描述

我无法在此函数中收集查询结果:

<?php
function getBestScore()
{
    require_once ('connection.php');

    $idJoueur = session_id();

    if ($stmt = $conn->prepare('SELECT bestScore FROM Player WHERE ID = ?'))
    {
        $stmt->bind_param('i', $idJoueur);
        $stmt->execute();
        $result = $stmt->get_result();

        $data = $result->fetch_assoc();

        $bestScore = $data;

        if (!(empty($bestScore)))
        {
            header("Location: ../index.php");
        }

        return $bestScore;
    }
    else
    {
        $error = $conn->errno . ' ' . $conn->error;
        echo $error;
    }

}

我的连接有效,我尝试过fetch_assoc()store_result()但每次都失败。

标签: phpmysqli

解决方案


如果要选择数据,请尝试以下操作

if ($stmt = $conn->prepare('SELECT bestScore FROM Player WHERE ID = ?')) {
    $stmt->bind_param('i',$idJoueur);
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($result);

    /* fetch value */
    $stmt->fetch();

    if (!(empty($result))) {
        header("Location: ../index.php");
    }

    return $result;
}

希望这可以帮助。


推荐阅读