douba8758 2013-04-19 04:36
浏览 189
已采纳

如何在PHP函数中返回while循环变量中的mysqli_fetch_assoc?

I have this PHP function :

function userParent($Username)
{
global $con;
$Result = mysqli_query($con, "SELECT Username FROM Family WHERE Parent = '$Username' LIMIT 10");
$row = mysqli_fetch_assoc($Result);
return $row; 
}

that function should give me 10 rows in array, but why I just have a value in array? I tried to test that codes outside function bracket and try to add WHILE loop like this :

while ($row = mysqli_fetch_assoc($Result)){
    print_r($row);
}

and it works. I got my 10 rows in array format. but it prints the result to the screen. how to make it as variable so it can be returned in function?

thanks.

UPDATE : according to Phil's answer, now here's my complete code :

<?php
function userParent(mysqli $con, $username) {
    $stmt = $con->prepare('SELECT Username FROM Family WHERE Parent = ? LIMIT 10');
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $res = $stmt->get_result();
    return $res->fetch_all(MYSQLI_ASSOC);
}

$DbServer = 'localhost';
$DbUser = 'username';
$DbPassword = 'password';
$DbName = 'dbname';
$mysqli = new mysqli($DbServer, $DbUser, $DbPassword, $DbName);

$arrayParent = userParent($mysqli, 'root');
print_r($arrayParent);

?>

but I got this error message :

Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/myhome/public_html/test.php on line 6

  • 写回答

2条回答 默认 最新

  • drqja5919276 2013-04-19 04:43
    关注

    Try mysqli_result::fetch_all instead

    function userParent(mysqli $con, $username) {
        $stmt = $con->prepare('SELECT Username FROM Family WHERE Parent = ? LIMIT 10');
        if ($stmt === false) {
            throw new Exception($con->error, $con->errno);
        }
        $stmt->bind_param('s', $username);
        $stmt->execute();
        $res = $stmt->get_result();
        return $res->fetch_all(MYSQLI_ASSOC);
    }
    

    Then call it like this

    $parents = userParent($mysqli, 'some username');
    

    Read these in case you're not aware of prepared statements and parameter binding

    Update

    Apparently (undocumented), the mysqli_stmt::get_result() method is only available when using the mysqlnd driver. If you cannot use this driver, try this alternative

    function userParent(mysqli $con, $username) {
        $stmt = $con->prepare('SELECT Username FROM Family WHERE Parent = ? LIMIT 10');
        if ($stmt === false) {
            throw new Exception($con->error, $con->errno);
        }
    
        $stmt->bind_param('s', $username);
        $stmt->execute();
    
        $parent = null;
        $parents = array();
        $stmt->bind_result($parent);
        while($stmt->fetch()) {
            $parents[] = $parent;
        }
        return $parents;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化