For a school project, I am creating an imaginary online banking site that acessess a database at school for imaginary account information. Every time I view account I get two mysqli errors. one on line 26 in person.class.php and another on line 52 on veiwaccounts.php I traced the main cause of that error in my person class but can’t seem to fix it in any way. The errors are
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in …/person.class.php on line 26
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in …/viewaccounts.php on line 52
Here is a sample of some code from the viewaccounts.php file:
if($currentMember)
{
$currentMember = new Person($memberid);
$accounts = $currentMember->retrieve_all_accounts();
//HERE IS WHERE THE PROBLEM IS $accounts = $currentMember->retrieve_all_accounts();
//Loop through accounts
while($account = mysqli_fetch_assoc($accounts)) {
//Retrieve balance
$bankaccount = new Bankaccount($account['BankAccountID']);
$bankaccount->connection = $conn;
$balance = mysqli_fetch_assoc($bankaccount->retrieve_current_balance());
Here is the code for the person class file:
public function retrieve_all_accounts() {
$accounts_query = "SELECT BankAccountID FROM BankAccount WHERE UserID = " .$this->memberid;
$result = mysqli_query($this->connection, $accounts_query);
return $result;
}
Change
To
I would also shift this into the first PHP code block for readability.
You are instantiating a new
Persononly if$currentMemberevaluates toTRUE– if theunserialize()call fails$currentMemberwill evaluate toFALSE, and that is when you should create a new object.Also you assigned the
connectionproperty first, which will be destroyed when you create the newPerson, which you are presumably doing every time otherwise you would see more warnings.You should also refactor the entire logic that tests whether the
Personwas saved in the$_SESSION– for a start, sessions will serialize objects themselves so you should not need to callserialize()/unserialize(). What you should do instead is define an__autoload()function and just store the object instance in$_SESSION.I also note that your development server seems to have
short_open_tagon – turn it off, it is bad practice to use it as it is disabled pretty much everywhere in the real world.