PHP Classes

Best PHP mysql to mysqli or PDO solution: How to convert mysql code to use mysqli?

Recommend this page to a friend!
  All requests RSS feed  >  Best PHP mysql to mysqli or PDO solution  >  Request new recommendation  >  A request is featured when there is no good recommended package on the site when it is posted. Featured requests  >  No recommendations No recommendations  

Best PHP mysql to mysqli or PDO solution

Edit

Picture of mimso by mimso - 7 years ago (2016-12-05)

How to convert mysql code to use mysqli?

This request is clear and relevant.
This request is not clear or is not relevant.

+4

I got php file in mysql and didn't know how to convert them

<?php

$host='127.0.0.1';

$uname='root';

$pwd='';

$db="cnb";

$con = mysql_connect($host,$uname,$pwd) or die("connection failed");

mysql_select_db($db,$con) or die("db selection failed");

$secnbid = $_REQUEST['se_cnbid'];

$seuid = $_REQUEST['se_uid'];

if($secnbid == 1) {

$r=mysql_query("select * from admin where ad_id = '$seuid'",$con);

while($row=mysql_fetch_array($r))
{
	$flag['se_name']=$row['ad_name'];
}

$flag['se_cnb'] = 'Admin';

}

if($secnbid == 2) {

$r=mysql_query("select * from hod where ho_id = '$seuid'",$con);

while($row=mysql_fetch_array($r))
{
	$flag['se_name']=$row['ho_name'];
}

$flag['se_cnb'] = 'HOD';

}

if($secnbid == 3) {

$r=mysql_query("select * from staff where stf_id = '$seuid'",$con);

while($row=mysql_fetch_array($r))
{
	$flag['se_name']=$row['stf_name'];
}

$flag['se_cnb'] = 'Staff';

}

if($secnbid == 4) {

$r=mysql_query("select * from student where std_id = '$seuid'",$con);

while($row=mysql_fetch_array($r))
{
	$flag['se_name']=$row['std_name'];
}

$flag['se_cnb'] = 'Student';

}

print(json_encode($flag));

mysql_close($con);

?>

  • 2 Clarification requests
  • 4. Picture of Dave Smith by Dave Smith - 7 years ago (2016-12-06) Reply

    The $_REQUEST super-global contains the key value pairs passed in through a form or in the uri. The code above gets the number of rows that match the value, so if it is going to remain dynamic then you need to be using the $_REQUEST, $_POST or $_GET super-globals.

    If you are experiencing problems, I suspect it is a typo in your query. Assuming that the column matches the request key, then in the query 'where re_cndid' should actually be 'where re_cnbid'.

    Also, since you are only getting one row, the count of rows that match in this case, you do not need to fetch the results within a loop. Your code using mysqli would actually be this...

    $r=mysqli_query($con,"select count(*) from header where re_cnbid = '$recnbid'");

    $row=mysqli_fetch_array($r);

    $flag['count']=$row[0];

    Keep in mind that I am working with the assumption that the table column is re_cnbid.

    Dave

    • 1. Picture of Dave Smith by Dave Smith - 7 years ago (2016-12-06) Reply

      <?php $host='127.0.0.1'; $uname='root'; $pwd=''; $db="cnb";

      $con = mysqli_connect($host,$uname,$pwd,$db) or die("connection failed");

      $secnbid = $_REQUEST['se_cnbid']; $seuid = $_REQUEST['se_uid'];

      $proc = true; switch($secnbid){ case 1: $query = "select * from admin where ad_id = '$seuid'"; $col = 'ad_name'; $flag['se_cnb'] = 'Admin'; break; case 2: $query = "select * from hod where ho_id = '$seuid'"; $col = 'ho_name'; $flag['se_cnb'] = 'HOD'; break; case 3: $query = "select * from staff where stf_id = '$seuid'"; $col = 'stf_name'; $flag['se_cnb'] = 'Staff'; break; case 4: $query = "select * from student where std_id = '$seuid'"; $col = 'std_name'; $flag['se_cnb'] = 'Student'; break; default: $proc = false; }

      if($proc){

      $r = mysqli_query($con,$query); $row = mysqli_fetch_assoc($r); $flag['se_name'] = $row[$col];

      print(json_encode($flag));

      }else{ //request out of range } ?>

      • 2. Picture of Dave Smith by Dave Smith - 7 years ago (2016-12-06) in reply to comment 1 by Dave Smith Comment

        <?php

        $host='127.0.0.1';

        $uname='root';

        $pwd='';

        $db="cnb";

        $con = mysqli_connect($host,$uname,$pwd,$db) or die("connection failed");

        $secnbid = $_REQUEST['se_cnbid'];

        $seuid = $_REQUEST['se_uid'];

        $proc = true;

        switch($secnbid){

        case 1:

        $query = "select * from admin where ad_id = '$seuid'";

        $col = 'ad_name';

        $flag['se_cnb'] = 'Admin';

        break;

        case 2:

        $query = "select * from hod where ho_id = '$seuid'";

        $col = 'ho_name';

        $flag['se_cnb'] = 'HOD';

        break;

        case 3:

        $query = "select * from staff where stf_id = '$seuid'";

        $col = 'stf_name';

        $flag['se_cnb'] = 'Staff';

        break;

        case 4:

        $query = "select * from student where std_id = '$seuid'";

        $col = 'std_name';

        $flag['se_cnb'] = 'Student';

        break;

        default:

        $proc = false;

        }

        if($proc){

        $r = mysqli_query($con,$query);

        $row = mysqli_fetch_assoc($r);

        $flag['se_name'] = $row[$col];

        print(json_encode($flag));

        }else{

        //request out of range

        }

        ?>

      • 3. Picture of mimso by mimso - 7 years ago (2016-12-06) in reply to comment 1 by Dave Smith Comment

        thank you very much. this solve most of the errors.

        do you have any suggestion if i want to remove the $_REQUEST syntax? for this code below?

        <?php

        $host='127.0.0.1';

        $uname='root';

        $pwd='';

        $db="cnb";

        $con = mysql_connect($host,$uname,$pwd) or die("connection failed");

        mysql_select_db($db,$con) or die("db selection failed");

        $recnbid = $_REQUEST['re_cnbid'];

        $r=mysql_query("select count(*) from header where re_cndid = '$recnbid'",$con);

        while($row=mysql_fetch_array($r)) {

        $flag['count']=$row[0];
        

        }

        print(json_encode($flag));

        mysql_close($con);

        ?>

        i tried to change the $_REQUEST syntax because its cause too much problem. do you have any suggestion for me?

    Ask clarification

    2 Recommendations

    MySQL Class Generator: Generate classes to access MySQL as objects

    This recommendation solves the problem.
    This recommendation does not solve the problem.

    0

    Picture of Saro Carvello by Saro Carvello package author package author Reputation 430 - 7 years ago (2017-02-14) Comment

    This package can automatically generate the classes source code for all database tables. It uses the mysqli object.


    PHP MySQL to MySQLi: Replace mysql functions using the mysqli extension

    This recommendation solves the problem.
    This recommendation does not solve the problem.

    +4

    Picture of Manuel Lemos by Manuel Lemos Reputation 23770 - 7 years ago (2016-12-06) Comment

    The fastest way to migrate your code to work with mysqli is to use this package that provides mysql replacement functions that internally use mysqli functions.

    • 1 Comment
    • 1. Picture of mimso by mimso - 7 years ago (2016-12-06) Reply

      wow! what a great effort!!!! thanks for the recommendation.


    Recommend package
    : 
    :