mysqli_query null error

Hi hows it going, I tried switching to mysqli, but not working out as great as I hoped.
I get a : Warning: mysqli_query() expects parameter 1 to be mysqli, null given in **** line 35 36 37
which is the mysqli_query below statements.
What might be wrong with it?

Thanks
Chad

function init(){

	
	session_start();
	if (isset($_SESSION['active']) != true) {
		$_SESSION['active'] = true;
	}
	$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
	
	if(isset($_GET['logout'])){
		session_destroy();
		header( 'Location: /index.php' ) ;
	
	}	
	
	
	//connect db
	$con = mysqli_connect("localhost", "dbuserr", "password", "dbname");
	
	if (mysqli_connect_errno())
  	{
		echo mysqli_connect_error();
		exit();
  	}

	
	global $siteName;
	global $adminEmail;
	global $siteUrl;
	$adminEmail = mysqli_query($con, "SELECT email FROM admin LIMIT 1");
	$siteUrl = mysqli_query($con, "SELECT url FROM admin LIMIT 1");
	$siteName = mysqli_query($con, "SELECT name FROM admin LIMIT 1");
}

I’m not seeing anything particularly off, have you double checked your database password, user, location information?

Also three things of note;

If this is going to be initialized on every call of the class it’s in just make those names match, that automatically does it. So if your class is named TESTCLASS naming the function TESTCLASS automatically runs it every time the class is used.

Generally it’s a good idea to avoid globals, since this is clearly user specfic information why not use Session variables instead if it’s something you’ll be using across pages or as a Class VAR if it’s only in this class.

Is there a particular reason you’re accessing the database three times instead of just doing something like below? Best practice is to minimize queries whenever possible, you’ll appreciate being in the habit of it later as that’s usally the biggest bottleneck.


$result =  mysqli_query($con,"SELECT name, email, url FROM admin LIMIT 1");
$row = mysqli_fetch_array($result);

 $adminEmail = $row['email'];
 $siteUrl = $row['url'];
 $siteName = $row['namel'];


This code is in the function.php file, and every other php page is included to this page.
I have the init function to start the sql database.
I did what you said and added the code above, this is code now:

$con = mysqli_connect("localhost", "db_user", "password", "db_name"); 
init();

function init(){

	
	session_start();
	if (isset($_SESSION['active']) != true) {
		$_SESSION['active'] = true;
	} 
	$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
	
	if(isset($_GET['logout'])){
		session_destroy();
		header( 'Location: /index.php' ) ;
	
	}	
	
	if (mysqli_connect_errno())
  	{
		echo mysqli_connect_error();
		exit();
  	}
$result =  mysqli_query($con, "SELECT name, email, url FROM admin LIMIT 1");
$row = mysqli_fetch_array($result);

 $adminEmail = $row['email'];
 $siteUrl = $row['url'];
 $siteName = $row['namel'];
}

I checked the database name and pass its the same, and Im not getting an error that is not connecting to the database via: mysqli_connect_errno

I’m getting these errors now:
mysqli_query() expects parameter 1 to be mysqli, null given in /home3/clfarmerfunction.php on line 25
mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home3public_html/function.php on line 26

which is the:

$result =  mysqli_query($con, "SELECT name, email, url FROM admin LIMIT 1");
$row = mysqli_fetch_array($result);
$query = "SELECT name, email, url FROM admin LIMIT 1";
$result = mysqli_query($con, $query);

/* associative array */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo $row['name'] . '<br>';
echo $row['email'] . '<br>';
echo $row['url'] . '<br>';
/* free result set */
mysqli_free_result($result);
/* close connection */
mysqli_close($con);


init($con);

function init($con){

You need to pass $con to the init method for it be be able to use it

Wow that did it, thank you so much!