0

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?

Gabriela Dias
  • 349
  • 2
  • 12

3 Answers3

0

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");

Braith Vince
  • 21
  • 1
  • 3
0

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());
}
Artem Ushakov
  • 313
  • 2
  • 12
0

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)) { ... }
ballbern
  • 116
  • 2
  • 3
  • 13
  • I use something like this: $users = mysql_query("select * from user limit 12"); while($usersfetch = mysql_fetch_array($users)) { ... } my mysql_query do not have any database name and it works. mysqli will not work? – Gabriela Dias Apr 05 '15 at 12:23