Jump to content

connecting to database (mysqli)


garry

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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, :).

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
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.