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.