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条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮