Jump to content

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.