garry Posted June 19, 2008 Share Posted June 19, 2008 Uhh, I just posted a thread before but I need some more help. I just switched over to mysqli from mysql and need some help with my function function db_connect() { // Set the information we need to connect to the server and database $host = "localhost"; $user = "xxxxxx"; $pass = "xxxxxx"; $db = "dev"; $mysqli = mysqli_connect($host, $user, $pass, $db); if (!$mysqli) { die ('MySQL connection could not be established.'); } return $mysqli; } that's my db_connect function. So then I call this function in another document and then use this code $query =" SELECT artists.id AS artistid, artists.artist, genres.genre FROM artists, genres WHERE artists.genre_id = genres.id "; $result = $mysqli->query($query); I get the following error: Notice: Undefined variable: mysqli in /htdocs/dev/artists.php on line 101 Why is the variable undefined? What am I doing wrong? Thanks for any help Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 19, 2008 Share Posted June 19, 2008 which is line 101? what variable is undefined? i dont know where to look at. Quote Link to comment Share on other sites More sharing options...
garry Posted June 19, 2008 Author Share Posted June 19, 2008 it's the $result = $mysqli->query($query); for some reason, it's not getting the $mysqli variable from the db_connect() function Quote Link to comment Share on other sites More sharing options...
garry Posted June 19, 2008 Author Share Posted June 19, 2008 nobody knows this? Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 19, 2008 Share Posted June 19, 2008 can you show the previous lines? Quote Link to comment Share on other sites More sharing options...
garry Posted June 19, 2008 Author Share Posted June 19, 2008 The lines there were the only relevant ones, everything else does other stuff with the page. Basically all I need is a function that connects to mysqli and selects the database so I can just call that function on any page. Can someone help? Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 19, 2008 Share Posted June 19, 2008 i think its somewhere else before... since there are only three variables declared: $query -- definitely assigned $result -- is to be assigned the only variable left is the object $mysqli... were you able to instantiate this object in the previous lines before using it? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 19, 2008 Share Posted June 19, 2008 You are trying to mix a Procedural style mysqli_connect with an Object oriented style mysqli_query. Read the two different type examples on the mysqli_query page - http://www.php.net/manual/en/mysqli.query.php Quote Link to comment Share on other sites More sharing options...
garry Posted June 19, 2008 Author Share Posted June 19, 2008 Okay, say I want to use the object styled one. I have this function function mysqli_conn() { $host = "localhost"; $user = "xxxxxx"; $pass = "xxxxxx"; $db = "dev"; $mysqli = new mysqli($host, $user, $pass, $db); } How can I call this function in another document and be able to use my $mysqli variable? Quote Link to comment Share on other sites More sharing options...
garry Posted June 19, 2008 Author Share Posted June 19, 2008 Okay, turns out I just needed to put "global $mysqli;" at the top of the function to make the variable global. Is this fine to do security wise? Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 20, 2008 Share Posted June 20, 2008 there is a procedural implementation of mysqli_*(), try using it... its much like mysql_*(), just the changed order of parameters and some non-optional parameters that was optional in mysql_*(). i actually use mysqli_*(), the procedural one, and created a separate class for my connection, which happens to be an easier for me... really reusable, . Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 20, 2008 Share Posted June 20, 2008 Okay, turns out I just needed to put "global $mysqli;" at the top of the function to make the variable global. Is this fine to do security wise? Security wise, there is probably no harm done. However, it's generally considered bad practice to pollute the global name space (at least as much as you can consider it a name space in php...). Why not just return the mysqli object from your function? function mysqli_conn() { $host = "localhost"; $user = "xxxxxx"; $pass = "xxxxxx"; $db = "dev"; return new mysqli($host, $user, $pass, $db); } function query($sql, $db_connection) { return mysqli_query($sql, $db_connection); } $database = mysqli_conn(); $result = query("SELECT * FROM tablename", $database); Quote Link to comment Share on other sites More sharing options...
garry Posted June 20, 2008 Author Share Posted June 20, 2008 Ah but I'm using the object oriented approach, so like: $result = $mysqli->query($query); Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 20, 2008 Share Posted June 20, 2008 Ah but I'm using the object oriented approach, so like: makes no difference... function mysqli_conn() { $host = "localhost"; $user = "xxxxxx"; $pass = "xxxxxx"; $db = "dev"; return new mysqli($host, $user, $pass, $db); } function query($sql, $db_connection) { return $db_connection->query($sql); } $database = mysqli_conn(); $result = query("SELECT * FROM tablename", $database); Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 20, 2008 Share Posted June 20, 2008 One of the main purposes of writing and using functions is so that you can call them any number of times in a piece of code. By using the global keyword to make a main program variable accessible inside the function, you have tied that function to that variable and you cannot reuse the function in the code. For your db_connect example, what happens if you should write a complex application and end up needing to connect to a second database? You cannot call that function again as it would overwrite the existing $mysqli variable. The function should return the value and you assign it to a main program variable like the examples that hitman6003 has been posting. Functions are intended to receive any optional input values through parameters in the function call, perform the desired processing, and then return (or output) the results. This allows functions to be reused without any worry of them conflicting with any code you put them into. Consider the case of all of the php built-in functions. None of them operate by requiring you to use a specifically named main program variable, which they then access using the "global" keyword inside of the function. Quote Link to comment 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.