Thomas Smyth Thomas Smyth - 11 days ago 5
PHP Question

"array_push() expects parameter 1 to be array" error message

I'm working on an Activation Queue for new accounts on my website.

The system will work by iterating through an array with each new user account details in and then display them so the Admin can either accept or deny the account. To collect the account details and display them in an array I'm using the following piece of code, however I get the error "array_push() expects parameter 1 to be array, null given". I have no clue why this is being caused and I have tried various things previously suggested. Thanks in advance.

<?php
session_start();

require "classes.php";
$TF = new TF_Core ();

$ActQueueQuery = "SELECT username, surname, forename, joined FROM users
WHERE rank = 'Unactivated'";

if ($statement = TF_Core::$MySQLi->DB->prepare($ActQueueQuery)) {
$statement->execute();
$results = $statement->get_result();
}

if($results->num_rows == 0){
$data = 1;
}
else{
$_SESSION["ActQueue"] = "";

while($row = $results->fetch_assoc()){
$_SESSION["ActQueue"] = array_push($_SESSION["ActQueue"], array($row["username"], $row["surname"], $row["forname"], $row["joined"]));
}
$data = 0;
}

echo $data;
?>

Answer
<?php
session_start();
$_SESSION["ActQueue"] = array(); // define an empty array

require "classes.php";
$TF = new TF_Core ();

$ActQueueQuery = "SELECT username, surname, forename, joined FROM users
WHERE rank = 'Unactivated'";

if ($statement = TF_Core::$MySQLi->DB->prepare($ActQueueQuery)) {
    $statement->execute();
    $results = $statement->get_result();
}
if($results->num_rows == 0){
    $data = 1;
}
else{
    while($row = $results->fetch_assoc()){
        $_SESSION["ActQueue"][] = array($row["username"], $row["surname"], $row["forname"], $row["joined"]); // check the change
    }
        $data = 0;
}
echo $data;
?>