Jump to content

Mysqli helping coding


pascal_22

Recommended Posts

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!!

Link to comment
Share on other sites

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

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.