Thursday, May 2, 2013

PHP Mysql to Mysqli mistakes

PHP Mysql to Mysqli mistakes

Converting from PHP Mysql statements to Mysqli statements I found myself making certain mistakes.

While the Mysqli procedural style resembles the mysql style, yet if you simply convert mysql_ to mysqli_ things won't work that easily ! I will try to list the errors and the actions I took for shifting to PHP procedural style mysqli statement.



mysqli_query expects parameter 1 to be mysqli

Error "mysqli_query() expects parameter 1 to be mysqli" was thrown and made me waste 2 days finding this silly mistake.
When we use mysql_query() we simply place the query string first followed by the link identifier(connection)
<?php
$link = mysql_connect('localhost','user','password') or die(mysql_error());
$q= mysql_query("Query string",$link) or die(mysql_error());
var_dump(mysql_fetch_array ($q));
?>
But when using mysqli_query(), the link comes first followed by the query string. Simple PHP Mysqli code in procedural style
<?php
$link = mysqli_connect('localhost','user','password') or die(mysqli_error());
$q= mysqli_query($link,"Query string") or die(mysqli_error());
var_dump(mysqli_fetch_array ($q));
?>
Please note that I use the Database.Table format in my query strings and hence the Database details are omitted in Mysql/Mysqli connect.

mysqli_real_escape_string expects exactly 2 parameters

mysqli_real_escape_string expects exactly 2 parameters
When migrating from mysql to mysqli, if you were using mysql_real_escape_string for escaping the data then you need to add an additional parameter, the link identifier(connection) to mysqli_real_escape_string else Error "mysqli_real_escape_string expects exactly 2 parameters 1 given" will result. so simple PHP Mysql Code
<?php
$link = mysql_connect('localhost','user','password') or die(mysql_error());
$string="string to escape";
$escaped_string=mysql_real_escape_string($string) or die(mysql_error());
echo $escaped_string;
?>
But when using mysqli_real_escape_string(), the link comes first followed by the string to escape. Simple PHP Mysqli code in procedural style
<?php
$link = mysqli_connect('localhost','user','password') or die(mysqli_error());
$string="string to escape";
$escaped_string=mysqli_real_escape_string($link,$string) or die(mysqli_error());
echo $escaped_string;
?>

mysqli_affected_rows() expects exactly 1 parameter

In mysql we simply add mysql_affected_rows() with or without any link identifier(connection) but mysqli wants link identifier as a parameter. Simple PHP Mysqli code in procedural style
<?php
$link = mysqli_connect('localhost','user','password') or die(mysqli_error());
$q= mysqli_query($link,"Update Query string") or die(mysqli_error());
if(mysqli_affected_rows($link) !==0){echo 'Row Updated';}
?>

Do not mix mysqli and mysql statements

While it is tempting to mix mysql and mysqli statements just to make your scripts work, do not do that! When you mix both then you are making it more difficult to track errors. The biggest disadvantage of using mysql is that PHP developers may later remove complete support for mysql statements, rather they recommend themselves to switch from mysql to mysqli.

For more posts like these visit www.internet-programs.com/forum/