First alter your select statement:
select field1, field2, ... from tbl_user where username = ....
then:
$rows = mysql_fetch_assoc($query_exec);
// you will get JSON data of all fields used in your selection
$jsonData = json_encode($rows);
سؤال
I would like to return a JSON objet containing informations retrieved from my database. Today, i only check that the username/password couple is correct and return a string "success".
The structure of tbl_user table is the following :
================================
============ tbl_user ==========
================================
- id
- username
- password
- name
- surname
- city
================================
================================
If the username/password couple exist in database, i would like to return a JSON object like that :
{
username : ..,
name : ..,
surname : ..,
city : ..
}
How can i do this ?
Here is my PHP code :
<?php
$hostname_localhost ="localhost";
$database_localhost ="database";
$username_localhost ="myusername";
$password_localhost ="mypassword";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$username = $_POST['username'];
$password = $_POST['password'];
$query_search = "select * from tbl_user where username = '".$username."' AND password = '".$password. "'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
//echo $rows;
if($rows == 0) {
echo "fail";
}
else {
echo "success";
}
?>
المحلول
First alter your select statement:
select field1, field2, ... from tbl_user where username = ....
then:
$rows = mysql_fetch_assoc($query_exec);
// you will get JSON data of all fields used in your selection
$jsonData = json_encode($rows);
نصائح أخرى
Fetch your results in an array, then encode it to JSON as following:
echo json_encode($result);
Just use php json encode function for you output, like:
echo json_encode($rows);