Recommend this page to a friend! |
All requests | > | Best PHP mysql to mysqli or PDO solution | > | Request new recommendation | > | Featured requests | > | No recommendations |
by mimso - 7 years ago (2016-12-05)
+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) {
} if($secnbid == 2) {
} if($secnbid == 3) {
} if($secnbid == 4) {
} print(json_encode($flag)); mysql_close($con); ?> |
4. 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. 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. 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. 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?
0 | by Saro Carvello 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. |
+4 | by Manuel Lemos 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. by mimso - 7 years ago (2016-12-06) Reply
wow! what a great effort!!!! thanks for the recommendation.
Recommend package | |
|