pascal_22 Posted February 13, 2013 Share Posted February 13, 2013 Hello, I want to switch from mysql to mysqli. but i have some questions. Are there an advantage to use object oriented style:$mysqli=new mysqli(...) or procedural style : $link=mysqli_connect(.....) ? My actual website, use mysql and all are procedural style : mysql_connect, mysql_query, mysql_num_rows.......... after doing a query, i close the connection mysql_close($link)... and reopen it if i have another query, such as in a function or further in the same script.... so it means that i can open and close the connection a lot in a same script...... I read that i can create an object like ::$mysqli=new mysqli(...) and nerver close it, and if i call a function, i just send $mysqli to the function: callThisFunction($mysqli) and the connection will be automaticly close when the script finish Is that better to open,close,open,close........ and so??? or open once and never close it I want to use the best way for the persomance of entire website! Thanks for your help!! Quote Link to comment https://forums.phpfreaks.com/topic/274444-mysqli-helping-coding/ Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 You should not open and close the connection every time. Quote Link to comment https://forums.phpfreaks.com/topic/274444-mysqli-helping-coding/#findComment-1412255 Share on other sites More sharing options...
pascal_22 Posted February 13, 2013 Author Share Posted February 13, 2013 Thanks Jessica! For better performance and clean code i found an article at here in fact: here the code i fount $DB_host = 'mysql_host'; // mysql host $DB_login = "mysql_username"; // mysql login $DB_password = "mysql_password"; // mysql password $DB_database = "mysql_database"; // the database which can be used by the script. $conn = new mysqli($DB_host,$DB_login,$DB_password,$DB_database); if (mysqli_connect_errno()) { printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error()); exit; } function mysqltng_query($query,$connection='') { global $conn; if ($connection=='') $connection=$conn; return $connection->query($query); } function mysqltng_fetch_assoc($result) { return $result->fetch_assoc(); } function mysqltng_result($result, $pos, $field) { $i=0; $retval=''; while ($row = $result->fetch_array(MYSQLI_BOTH)) { if ($i==$pos) $retval=$row[$field]; $i++; } return $retval; } function mysqltng_num_rows($result) { return $result->num_rows; } function mysqltng_data_seek($result,$offset) { $result->data_seek($offset); } function mysqltng_free_result($result) { $result->free; } function mysqltng_close($connection='') { global $conn; if ($connection=='') $connection=$conn; $connection->close; } Is that a good way to include the file that open connection in the beginning of script (with a couple a good function in it) and when a want create a query, i call the finction that create the query by passing sql string in param. Is that a good and clean pratice? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/274444-mysqli-helping-coding/#findComment-1412261 Share on other sites More sharing options...
pascal_22 Posted February 13, 2013 Author Share Posted February 13, 2013 Calling an include that open a connection in the beginning of all pages with no connection close... is that a wrong or a good idea? Some forum say it's a good idea to dont close it, because it take ressource too. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/274444-mysqli-helping-coding/#findComment-1412324 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.