White_Lily Posted February 13, 2013 Share Posted February 13, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/274454-mysqli-functions/ Share on other sites More sharing options...
Barand Posted February 13, 2013 Share Posted February 13, 2013 You need to read up on variable scope. Your connect() function uses $connections so you need to pass it as a function parameter. Quote Link to comment https://forums.phpfreaks.com/topic/274454-mysqli-functions/#findComment-1412314 Share on other sites More sharing options...
White_Lily Posted February 13, 2013 Author Share Posted February 13, 2013 (edited) 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 February 13, 2013 by White_Lily Quote Link to comment https://forums.phpfreaks.com/topic/274454-mysqli-functions/#findComment-1412316 Share on other sites More sharing options...
Barand Posted February 13, 2013 Share Posted February 13, 2013 your connect function doesn't return the connection. It returns either true or false. Quote Link to comment https://forums.phpfreaks.com/topic/274454-mysqli-functions/#findComment-1412322 Share on other sites More sharing options...
White_Lily Posted February 13, 2013 Author Share Posted February 13, 2013 haha oh yeah :S fixed it! thanks Barand x Quote Link to comment https://forums.phpfreaks.com/topic/274454-mysqli-functions/#findComment-1412323 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.