PHP Classes
elePHPant
Icontem

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

by mimso - Yesterday (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.

+1

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

?>

  • 1 Clarification request
  • 1. by Dave Smith - 3 hours 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. by Dave Smith - 3 hours 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

      }

      ?>

Ask clarification

1 Recommendation

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

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

+1

by Manuel Lemos Reputation 16150 - 4 hours 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.


Recommend package
: 
: