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.
-
YOU MAY ALSO LIKE
- FILE : PHP Mysql to Mysqli mistakes | PHP Mysql to Mysqli mistakes
- 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 diffi ... - MYSQL TUTORIAL FILES
- 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 simpl ... - PHP TUTORIAL FILES
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)<?phpBut when using mysqli_query(), the link comes first followed by the query string. Simple PHP Mysqli code in procedural style
$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));
?><?phpPlease note that I use the Database.Table format in my query strings and hence the Database details are omitted in Mysql/Mysqli connect.
$link = mysqli_connect('localhost','user','password') or die(mysqli_error($link));
$q= mysqli_query($link,"Query string") or die(mysqli_error($link));
var_dump(mysqli_fetch_array ($q));
?>
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<?phpBut when using mysqli_real_escape_string(), the link comes first followed by the string to escape. Simple PHP Mysqli code in procedural style
$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;
?><?php
$link = mysqli_connect('localhost','user','password') or die(mysqli_error($link));
$string="string to escape";
$escaped_string=mysqli_real_escape_string($link,$string) or die(mysqli_error($link));
echo $escaped_string;
?>
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($link));
$q= mysqli_query($link,"Update Query string") or die(mysqli_error($link));
if(mysqli_affected_rows($link) !==0){echo 'Row Updated';}
?>
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.
MYSQL TUTORIAL PHP TUTORIAL