Jump to content

Question about connecting to database


programming.name

Recommended Posts

Hi,

 

I am having difficulties with connecting to database. I mean I can connect to databse pretty easily with the following code:

 

$db_conn = new MySQLi('localhost', 'root', '', 'db');

 

But the following code does not work when it comes with functions:

 

function a(){

$db_conn = new MySQLi('localhost', 'root', '', 'db');

}

 

function b(){

$result = $db_conn->query("INSERT INTO users(name) VALUES('$name')");

}

 

a();

b();

 

It seems when the function a() is called  mysql connection is on and as soon as the function b() is called the connection is closed.

Is there any way to solve this problem?

 

Thanks.

Link to comment
Share on other sites

A variable defined in one function does not exist in other functions.  In this case the $db_conn defined in function a() is not the same $db_conn you are trying to reference in function b().  There are two ways to resolve this problem:

 

1-Recommended (though not necessarily by me)

function a(&$db_conn) { 
  $db_conn = new MySQLi('localhost', 'root', '', 'db');
}

function b(&$db_conn){
  $result = $db_conn->query("INSERT INTO users(name) VALUES('$name')");
}

a($dbc);
b($dbc);

 

2-Not Recommended (but I use it often for my db connection)

function a() { 
  global $db_conn;
  $db_conn = new MySQLi('localhost', 'root', '', 'db');
}

function b(){
  global $db_conn;
  $result = $db_conn->query("INSERT INTO users(name) VALUES('$name')");
}

$db_conn = null;

a();
b();

Link to comment
Share on other sites

1-Recommended (though not necessarily by me)

function a(&$db_conn) { 
  $db_conn = new MySQLi('localhost', 'root', '', 'db');
}

function b(&$db_conn){
  $result = $db_conn->query("INSERT INTO users(name) VALUES('$name')");
}

a($dbc);
b($dbc);

 

How does he access $result then? ;)

 

You could also just write:

 

function a() { 
  return new MySQLi('localhost', 'root', '', 'db');
}

function b($db_conn){
  return $db_conn->query("INSERT INTO users(name) VALUES('$name')");
}

$dbc = a();
$result = b($dbc);

Link to comment
Share on other sites

1-Recommended (though not necessarily by me)

function a(&$db_conn) { ...

function b(&$db_conn){ ...

 

Reference operator (&) is not necessary here. MySQLi is an object and all objects are passed by reference.

Link to comment
Share on other sites

1-Recommended (though not necessarily by me)

function a(&$db_conn) { ...

function b(&$db_conn){ ...

 

Reference operator (&) is not necessary here. MySQLi is an object and all objects are passed by reference.

 

You will still need it in a() though as the variable does not yet hold a reference to a class and is thus passed as a copy. As shown in:

 

function a1(&$dbc) {
  $dbc = new StdClass();
}

function a2($dbc) {
  $dbc = new StdClass();
}

$dbc1 = null;
a1($dbc1);
var_dump($dbc1);//Returns: MySQLi Object
$dbc2 = null;
a2($dbc2);
var_dump($dbc2);//Returns: NULL

 

Anyway it's silly explaining this as it's bad to write your code like that.

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.