programming.name Posted May 18, 2010 Share Posted May 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/202111-question-about-connecting-to-database/ Share on other sites More sharing options...
DavidAM Posted May 18, 2010 Share Posted May 18, 2010 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(); Quote Link to comment https://forums.phpfreaks.com/topic/202111-question-about-connecting-to-database/#findComment-1059875 Share on other sites More sharing options...
ignace Posted May 18, 2010 Share Posted May 18, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/202111-question-about-connecting-to-database/#findComment-1059947 Share on other sites More sharing options...
Mchl Posted May 18, 2010 Share Posted May 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/202111-question-about-connecting-to-database/#findComment-1059961 Share on other sites More sharing options...
ignace Posted May 18, 2010 Share Posted May 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/202111-question-about-connecting-to-database/#findComment-1060001 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.