我实际上是将我的代码从mysql转换为mysqli。我有一个用mysql编写的代码,它将查询结果作为对象返回。我正在使用ExtJS4和MVCS架构来处理php文件返回的结果。这是原始的mysql php代码:
<?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());
?>
我所做的是创建一个对象来存储结果查询的行,我查询数据库,然后迭代结果集。在每次迭代期间,我创建一个holder对象并将值存储在那里,然后将其保存到另一个与响应对象一起传回的数组中。该代码效果很好。
但是,我现在正在使用mysqli,到目前为止,这是我尝试过的。请注意,我使用了相同的类,我只会显示查询的部分并将结果保存到对象。
//I used some hard coded values first
$query = "SELECT * FROM user_details
WHERE email = 'test@yahoo.com' AND password = '123'";
$result = $mysqli->query($query);
我试过这个:
$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());
我还试图保存结果集:
if ($result = $mysqli->query($query)) {
/* fetch object array */
while ($obj = $result->fetch_object()) {
$query_array[$i]=$obj;
$i++
}
}
但是,当我使用localhost:8888/../../../../../../readUser.php
从浏览器中调用它们时,我看不到print_r
函数调用的任何输出,这意味着我在某处有错误。
任何人都可以帮助解决我的小问题吗?很感谢任何形式的帮助。谢谢。
答案 0 :(得分:0)
道歉。我有一个迷路$res->message.= $result;
线,搞砸了很多东西。这是使它工作的代码。
//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