Jump to content

mysqli + functions.


White_Lily

Recommended Posts

Hey.

 

I am trying to start using mysqli and once I get used to using it and can pretty much be able to code with it without asking for too much help then I shall also start converting pre-existing sites over to mysqli to.

 

However, I have this problem where my connect() function can't actually connect.

 

Here is the config.php file that is (and will be) included on every single page:

 

<?php
$connections = array(
"siteHost" => "localhost",
"username" => "root",
"password" => "",
"database" => "ajax_website"
);

$site = array(
"siteUrl" => "http://".$_SERVER["HTTP_HOST"]."/Projects/aJax Driven Website"
);

$fileSys = array(
"maxSize" => 1048576,
"acceptedFiles" => "jpeg,jpg,png,bmp,gif,tiff,tif,svg",
"uploadPath" => $_SERVER["DOCUMENT_ROOT"]."/Projects/aJax%20Driven%20Website"
);
?>

 

and here is the functions.php file that is (and will) also be included on every single page:

 

 

<?php

function connect()
{
$connect = mysqli_connect($connections["siteHost"], $connections["username"], $connections["password"], $connections["database"]);

if($connect)
return true;
else
return false;
}

function select($table, $columns = "*", $where = NULL, $order = NULL, $limit = NULL)
{
$query = "SELECT ".$columns." FROM ".$table;

if($where != NULL)
$query.= " WHERE ".$where;
if($order != NULL)
$query.= " ORDER BY ".$order;
if($limit != NULL)
$query.= " LIMIT ".$limit;

if(!empty($query))
return mysqli_query(connect(), $query);
}
?>

 

this is how I am including them onto all my pages:

 

<?php
include "cms/inc/config.php";
include "cms/inc/functions.php";
$page = "Home";
include "inc/pageInc.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>...</html>

 

the errors I am receiving are:

 

Notice: Undefined variable: connections in C:\xampp\htdocs\Projects\aJax Driven Website\cms\inc\functions.php on line 5

 

Notice: Undefined variable: connections in C:\xampp\htdocs\Projects\aJax Driven Website\cms\inc\functions.php on line 5

 

Notice: Undefined variable: connections in C:\xampp\htdocs\Projects\aJax Driven Website\cms\inc\functions.php on line 5

 

Notice: Undefined variable: connections in C:\xampp\htdocs\Projects\aJax Driven Website\cms\inc\functions.php on line 5

 

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\Projects\aJax Driven Website\cms\inc\functions.php on line 25

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/274454-mysqli-functions/
Share on other sites

Okay Barand thank you, I have read up on variable scopes and declared the $connections variable to be global within the connect() function, and it all works now :)

 

EDIT: or I thought it had... it now says that mysqli_query expects parameter 1 to be mysqli, boolean given on line 28 of functions.php

Link to comment
https://forums.phpfreaks.com/topic/274454-mysqli-functions/#findComment-1412316
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.