This question already has an answer here:
i have a website that is online about 5 years. just now I noticed that i use mysql connection.
I want to change it to mysqli.
So, I have some questions. To change mysql to mysqli I just need to put "i" after all mysql words?
query
eg
$i=mysql_query("INSERT
to
$i=mysqli_query("INSERT
num rows:
$num = mysql_num_rows($rs);
to
$num = mysqli_num_rows($rs);
string escape:
mysql_real_escape_string
to
mysqli_real_escape_string
fetch arraw
$l = mysql_fetch_array($re);
to
$l = mysqli_fetch_array($re);
is that simple? or i need to know something else?
</div>
No it's not that simple..mysqli functions usually take a second parameter...so check the syntax of whatever functions you are changing. For instance..instead of mysql_connect("localhost", "my_user","my_password");
for mysqli you will use mysqli_connect("localhost","my_user","my_password","my_db");
This will actually guide you through: http://lv1.php.net/manual/en/class.mysqli.php
While mysql
was only the procedure-style function, the new mysqli
is both: the procedure-style and object-style.
You can use the object-oriented style, like:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
or procedural style:
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
The parameters of mysqli are reversed I.e. mysql_query($q, $dbc) becomes mysqli_query($dbc, $q). Also some functions take extra parameters, usually the connection . Php.net has a full list of all changes.
UPDATE:
//your connection script should look somthing like this.
$db_connnect = mysqli_connect('localhost','username','password','db_name');
//the page you require the connection on should look something like this.
require_once ('db_connect.php');
//a mysqli query would look something like this.
$q = "select * from user limit 12";
$r = mysqli_query("$db_connnect, $q");
while($row = mysqli_fetch_array($r)) { ... }