I have a list of functions in functions.php. I'm upgrading from Mysql to Mysqli because I just learning Mysql is now depreciated.
I declare my connection in a top levelconnect.php file. The first file required.
Anyway back to the point all my functions use mysql_query("QUERY") and that always worked fine. Now I changed all the them to:
$con->query("QUERY") // ($con is my connection variable)
Now I'm getting a Fatal error: Call to a member function query() on a non-object in C:\wamp\www\PHP\functions.php on line 241.
So I don't get why I can query if I'm declaring my variable in my whole file. It should be accessible everywhere, I'm just not sure. This has put my website on hold, until I can fix this. Here is a sample function from functions.php
function getSiteName()
{
$row = $con->query("SELECT * FROM siteinfo")->fetch_array();
return $row["siteName"];
}
My connection:
global $con ;
$con = new mysqli("localhost", "itunes89", "XXXX","projectanvil") or die("Sorry, were having server connection issues. Please try again later.");
Thanks guys! :D My site where I'm having this error
Do you defind for $con object
That's a variable scope problem. You need to pass
$conn
togetSiteName()
:Or using a class with constructor injection:
Throughout the class you can use
$this->con
to access the connection.Will not return null or false on failure.
You should check value of
After connection to verify that $con is valid server connection.
Since you made $con global, to make getSiteName function access it you should write: