Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change mysql to mysqli?

Tags:

php

mysql

mysqli

Based on this code below I use for regular mysql, how could I convert it to use mysqli?

Is it as simple as changing mysql_query($sql); to mysqli_query($sql);?

<?PHP  //in my header file that is included on every page I have this $DB["dbName"] = "emails"; $DB["host"] = "localhost"; $DB["user"] = "root"; $DB["pass"] = ""; $link = mysql_connect($DB['host'], $DB['user'], $DB['pass']) or die("<center>An Internal Error has Occured. Please report following error to the webmaster.<br><br>".mysql_error()."'</center>"); mysql_select_db($DB['dbName']); // end header connection part  // function from a functions file that I run a mysql query through in any page. function executeQuery($sql) {     $result = mysql_query($sql);     if (mysql_error()) {         $error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has Occured.<BR> The error has been recorded for review</font></center><br>';         if ($_SESSION['auto_id'] == 1) {             $sql_formatted = highlight_string(stripslashes($sql), true);             $error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted . '<br><br><b>The MySQL Error Returned</b><br>' . mysql_error();         }         die($error);     }     return $result; }  // example query ran on anypage of the site using executeQuery function $sql='SELECT auto_id FROM friend_reg_user WHERE auto_id=' .$info['auto_id']; $result_member=executequery($sql); if($line_member=mysql_fetch_array($result_member)){     extract($line_member); } else {     header("location: index.php");     exit; } ?> 
like image 864
JasonDavis Avatar asked Sep 07 '09 18:09

JasonDavis


People also ask

Can I use MySQLi in MySQL?

The MySQLi Extension (MySQL Improved) is a relational database driver used in the PHP scripting language to provide an interface with MySQL databases. It provides both object oriented and procedural APIs. Other ways to interact with MySQL are: PDO and ORM solutions.

Is MySQL same as MySQLi?

Basically, MySQL is the old database driver, and MySQLi is the Improved driver. The "i" stands for "improved" so it is MySQL improved. MySQLi can be done procedural and object-oriented whereas MySQL can only be used procedurally. Mysqli also supports prepared statements which protect from SQL Injection.

How configure MySQLi in PHP?

It'll automatically enable the mysqli extension for the PHP because connect using mysql is deprecated in PHP 7. If not an Ubuntu user then you can just rename php-prodcution. ini file to php. ini and enable the extension by removing semicolon in the php.

Is MySQLi faster than MySQL?

The MySQL extension is very slightly faster than MySQLi in most benchmarks I've seen reported. The difference is so slight, however, that this should probably not be your criterion for deciding between the two.


2 Answers

The first thing to do would probably be to replace every mysql_* function call with its equivalent mysqli_*, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the MySQL API, which is a procedural one.

To help with that, the MySQLi Extension Function Summary is definitely something that will prove helpful.

For instance:

  • mysql_connect will be replaced by mysqli_connect
  • mysql_error will be replaced by mysqli_error and/or mysqli_connect_error, depending on the context
  • mysql_query will be replaced by mysqli_query
  • and so on

Note: For some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)

For instance:

  • with mysql, you have to use the mysql_select_db once connected, to indicate on which database you want to do your queries
  • mysqli, on the other side, allows you to specify that database name as the fourth parameter to mysqli_connect.
  • Still, there is also a mysqli_select_db function that you can use, if you prefer.

Once you are done with that, try to execute the new version of your script... And check if everything works ; if not... Time for bug hunting ;-)
like image 104
Pascal MARTIN Avatar answered Oct 08 '22 05:10

Pascal MARTIN


(I realise this is old, but it still comes up...)

If you do replace mysql_* with mysqli_* then bear in mind that a whole load of mysqli_* functions need the database link to be passed.

E.g.:

mysql_query($query) 

becomes

mysqli_query($link, $query) 

I.e., lots of checking required.

like image 20
dhw Avatar answered Oct 08 '22 05:10

dhw