使用PHP从MySQL中返回返回数组

时间:2015-01-21 03:44:15

标签: php mysql arrays

我正在尝试提取与登录到我网站的特定用户相关联的所有徽章。在我的index.php页面上,我有,

<?php
     include_once 'includes/db_connect.php';
     include_once 'includes/functions.php';
     print_r(get_badges($_SESSION['user_id'], $mysqli));
?>

我的db_connect.php:

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

在我的functions.php上,我有,

<?php
function get_badges($u, $mysqli){
     $badges[] = array();

     if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }

    $query = "SELECT * FROM memBadges WHERE ownerId = $u";

    if ($result = $mysqli->query($query)) {

        while ($row = $result->fetch_assoc()) {
            $badges[] = $row["badgeId"]);
        }
    }

$mysqli->close();

return $badges;
}
?>

问题是functions.php页面中的部分使我的页面变为空白。当我删除它时,页面看起来很正常。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的代码中存在语法错误。改变

$badges[] = $row["badgeId"]);

$badges[] = $row["badgeId"];

并且还要改变

$badges[] = array();$badges = array();