0

I am trying to pull all badges associated to a particular user that is logged into my site. On my index.php page I have,

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

My db_connect.php:

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

and on my functions.php I have,

<?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;
}
?>

The problem is that the piece in the functions.php page makes my page go blank. When I remove it, the page looks normal. What am I doing wrong?

5
  • 1
    You have an error in your page. Turn on error reporting Commented Jan 21, 2015 at 3:45
  • maybe there is a syntax error somewhere, but the message is suppressed by the server? Commented Jan 21, 2015 at 3:45
  • 1
    How do you use $mysqli inside the function get_badges() if it is not defined as global ? Commented Jan 21, 2015 at 3:46
  • This is up on a hosted server and I added " ini_set('display_errors',1); error_reporting(E_ALL); " and I'm not seeing any error reporting, even in the error_log. Commented Jan 21, 2015 at 4:00
  • @Prokzy, try debuggung. Add echo 123; exit; as the first line of the script. Run. See 123? Now move it a line below. Continue until you know what causes the script to die. Tell us. Commented Jan 21, 2015 at 4:13

1 Answer 1

1

You have syntax error in your code . change

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

to

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

and also change

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

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, I missed that. Thank you.
@Prokzy, Sure! Good luck coding

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.