PHP Mysql to Mysqli mistakes




2.13 K Views | Rishi Kashyap | 1 FOLLOW
    X

    PHP Mysql to Mysqli mistakes


    0

    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.PHP Mysql to Mysqli mistakes

    Rishi Kashyap | 7 years ago | EDIT | REPLY



      YOU MAY ALSO LIKE
      X mysqli_query expects parameter 1 to be mysqli
      0

      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($link));
      $q= mysqli_query($link,"Query string") or die(mysqli_error($link));
      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.

      Rishi Kashyap | 7 years ago | EDIT | REPLY
        X mysqli_real_escape_string expects exactly 2 parameters
        0

        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($link));
        $string="string to escape";
        $escaped_string=mysqli_real_escape_string($link,$string) or die(mysqli_error($link));
        echo $escaped_string;
        ?>

        Rishi Kashyap | 7 years ago | EDIT | REPLY
          X mysqli_affected_rows() expects exactly 1 parameter
          0

          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';}
          ?>

          Rishi Kashyap | 7 years ago | EDIT | REPLY
            X Do not mix mysqli and mysql statements
            0

            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.

            Rishi Kashyap | 7 years ago | EDIT | REPLY

              tags  MYSQL TUTORIAL PHP TUTORIAL