LuPaw LuPaw - 6 months ago 11
SQL Question

PHP mysqli functions fatal ERROR when using the query function

I am programming a website and my current task is to save the text on a mysql server so I can login as admin and then edit the displayed text online.

I already got so far that everything works but I have done it using mysql functions. Now changed all those functions to mysqli ones, since I heard those are way better.

Here is my mysql_connect.php which I created so I could include this in the other scripts wherever I need to connect to the server:

mysql_connect.php

This is my block1_aboutme.php file, where I connect to the server and request all the text from it so I can display it on my "about me" page. This also works as intended and it properly displays the text on my page.

block1_aboutme.php

The problem is when I login as an admin a part of the login verification is this code below in my class.login.php script. As soon as I am logged in and go to my aboutme page an error occurs:


Fatal error: Uncaught Error: Call to a member function query() on unknown in C:\xampp\htdocs\mywebsites\LuPaw\admin\class.login.php:84 Stack trace: #0 C:\xampp\htdocs\mywebsites\LuPaw\admin\class.login.php(73): Login->verifyDatabase() #1 C:\xampp\htdocs\mywebsites\LuPaw\admin\class.login.php(36): Login->verifySession() #2 C:\xampp\htdocs\mywebsites\LuPaw\aboutme.php(42): Login->isLoggedIn() #3 {main} thrown inC:\xampp\htdocs\mywebsites\LuPaw\admin\class.login.php on line 84


This error refers to line 84 which is the
$data = $db-> query("select stuff")
function.

Somehow this does not work here, while the nearly exact same code line does work in the block1_aboutme script.

I would place the link of my 3rd script here but I can't since I don't have reputation pints yet to place 3 links in one post. Will post 3rd picture in the comments.

I already tried out a few things. If I for example echo out the "$path" it shows the correct path which means $db should be a viable variavble.

Does anyone see the problem here?

Answer

Try not to use include_once in your class method as the file will not be included if anywhere during the script execution has been already included: use include instead. Anyway this is not the best solution you can achieve, I can suggest you to implement a DB class as a singleton and then retrieve the database instance in order to execute your queries.

Comments