dongxiai3003 2014-12-19 00:43
浏览 92

PHP5 - 将mysqli行结果保存为对象?

I am actually converting my code from mysql to mysqli. I have a code written in mysql that returns the query result(s) as an object. I am using ExtJS4 and the MVCS architecture to handle this result returned by the php file. Here is the original mysql php code:

<?php
require_once('../../db_mysqli.php');
require_once '../../lib/response.php';
require_once '../../lib/request.php';

class user{ 
    var $user_id;
    var $first_name;
    var $last_name;
    var $sex;
    var $birth_date;
    var $email;
    var $password;
}

$total;

if(isset($_GET['email'])){
    $email = $_GET['email'];
    $password = $_GET['password'];

    $query = "SELECT * FROM user_details 
                    WHERE email = '$email' AND password = '$password'";

    $result = mysql_query($query);
    $totalquery = mysql_query("SELECT COUNT(*) FROM user_details 
            WHERE email = '$email'
            AND password = '$password'");

    $total = mysql_fetch_array($totalquery);
}

$total =($total[0]);
$query_array=array();
$i=0;

//Iterate all Select
while($row = mysql_fetch_array($result)){
    //Create New User instance
    $inputData = new user();
    //Fetch User Info

    $inputData->user_id=$row['user_id'];        
    $inputData->first_name=$row['first_name'];
    $inputData->last_name=$row['last_name'];
    $inputData->sex=$row['sex'];
    $inputData->birth_date=$row['birth_date'];
    $inputData->email=$row['email'];
    $inputData->password=$row['password'];

    //Add User to ARRAY
    $query_array[$i]=$inputData;
    $i++;
}
mysql_close($con);

//Creating Json Array needed for Extjs Proxy
$res = new Response();
$res->success = true;
$res->message = "Loaded read data";
$res->total = $total;
$res->data = $query_array;
//Printing json ARRAY
print_r($res->to_json());
?>

What I do is that I create an object to store the rows of the result query, I query the database, after which, I iterate through the result set. During each iteration, I create a holder object and store the values there then save that to another array which gets passed back with the response object. That code works great.

However, I am now using mysqli and so far, this is what I've tried. Kindly note that I used the same class, and I will only be showing the part where I query and save the results to an object.

//I used some hard coded values first
$query = "SELECT * FROM user_details 
    WHERE email = 'test@yahoo.com' AND password = '123'";
$result = $mysqli->query($query);   

And I've tried this:

$query_array=array();
$i=0;

//Iterate all Select
while($row = $result->fetch_array(MYSQLI_ASSOC)){
    //Create New User instance
    $inputData = new user();
    //Fetch User Info

    $inputData->user_id=$row['user_id'];        
    $inputData->first_name=$row['first_name'];
    $inputData->last_name=$row['last_name'];
    $inputData->sex=$row['sex'];
    $inputData->birth_date=$row['birth_date'];
    $inputData->email=$row['email'];
    $inputData->password=$row['password'];

    //Add User to ARRAY
    $query_array[$i]=$inputData;
    $i++;
}

$result->close();
$mysqli->close();

$res = new Response();
$res->success = true;
$res->total = $total;
$res->data = $query_array;
print_r($res->to_json());

And I've also tried to save the result set as such:

if ($result = $mysqli->query($query)) {

    /* fetch object array */
    while ($obj = $result->fetch_object()) {
        $query_array[$i]=$obj;
        $i++
    }
}

However, when I call them from the browser using localhost:8888/../../../../../../readUser.php, I don't see any output from the print_r function call which means that I have an error there somewhere.

Can anyone help with my small problem? Any help is very much appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • doutanggun9816 2014-12-19 03:17
    关注

    Apologies. I had a stray $res->message.= $result; line and that screwed up a lot of things. Here was the code that made it work.

    //check for some variables that may or may not be passed 
    if(isset($_GET['email'])){
        $email = $_GET['email'];
        $password = $_GET['password'];
    
        $query = "SELECT * FROM user_details 
                        WHERE email = '$email' AND password = '$password'";     
        //execute query here
        $result = $mysqli->query($query);
        $total = 1;
    
    }
    
    //I used the associative style since I don't want to guess column index 
    while($row = $result->fetch_array(MYSQLI_ASSOC)){
        //Create New User instance
        $inputData = new user();
        //Fetch User Info
    
        $inputData->user_id=$row['user_id'];        
        $inputData->first_name=$row['first_name'];
        $inputData->last_name=$row['last_name'];
        $inputData->sex=$row['sex'];
        $inputData->birth_date=$row['birth_date'];
        $inputData->email=$row['email'];
        $inputData->password=$row['password'];
    
        //Add User to ARRAY
        $query_array[$i]=$inputData;
        $i++;
    }
    
    //close result set and sqli connection
    $result->close();
    $mysqli->close();
    
    //create your response object
    $res = new Response();
    $res->message = "Message Start. ";
    
    $res->success = true;
    $res->total = $total;
    $res->data = $query_array; //set the data to your query_array result
    print_r($res->to_json()); //send back to extJS
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题