PHP Array Conversion JSON There's a problem, everybody.

Source: Internet
Author: User
Tags deprecated



Why does the following remark not output anything?
echo Json_encode ($result); #输出结果

Personally, it may be because the array element inside the $data is the client object, and there is a problem when it goes to JSON. But I don't know what the problem is, how to convert it? Or is there any other method of conversion?
Class Result {var $state; var $data = array ();}

Full code:
$start = 0; $end = 2; $uid = 1; $result = new result (); $result _set = Getclients ($start, $end, $uid); $data = Array (); $count = 0;while ($row = mysql_fetch_array ($result _set)) {$client = new client (); $client->cid = $row [' id ']; $client->gender = $row [' Gender ']; $client->rank = $row [' rank ']; $client->name = $row [' name ']; $client->phonenum = $row [' Phonenum ']; $client->email = $row [' email ']; $client->address = $row [' address ']; $result->data[$count] = $ Client;echo $result->data[$count]->cid. "  "; echo $result->data[$count]->gender. "  "; echo $result->data[$count]->rank. "  "; echo $result->data[$count]->name. "  "; echo $result->data[$count]->phonenum. "  "; echo $result->data[$count]->address. " \ n "; $count + +;    } if (count ($data) > 0) {$result->state = 0;} else {$result->state = 1;} echo Json_encode ($result); #输出结果class result {var $state; var $data = array ();}

Output Result:
----------Debug PHP----------deprecated:mysql_connect (): The MySQL extension is Deprecated and would be a removed in the F  Uture:use mysqli or PDO instead in D:\WWW\wsd\trunk\src\server\api\db\init_db.php on line 10deprecated:mysql_connect (): The MySQL extension is deprecated and would be removed in the Future:use mysqli or PDO instead in D:\WWW\wsd\trunk\src\se Rver\api\db\init_db.php on line 103  0  manager  Zhangxiaoli  18825044888  Guangdong Guangzhou Tianhe District 4  0  supervisor  Wu Mifeng  18576430777,  No. 1079th, Nanhai Avenue, Shekou, Shenzhen, Nanshan District, Guangdong, China php Deprecated:  mysql_connect (): The MySQL extension is Deprecated and would be Removed in the Future:use mysqli or PDO instead in D:\WWW\wsd\trunk\src\server\api\db\init_db.php on line 10PHP deprecate  D:  mysql_connect (): The MySQL extension is deprecated and would be a removed in the Future:use mysqli or PDO instead in D:\WWW\wsd\trunk\src\server\api\db\init_db.php on line 10Output completed (0 sec consumed)-Normal termination


There should be output similar to this format in order to:
{"state": 1, "Data": [           {"CID": 3,          "gender": 0,          "rank": "Manager"          "name": "Zhangxiaoli"          "Phonenum": "18825044888  "          address": "Guangdong Province Guangzhou Tianhe District"},          {"CID": 4,          "gender": 0,          "rank": "Supervisor"          "name": "Wu Mifeng"          "Phonenum": "18576430777  "          "Address" : "1079th Nanhai Road, Shekou, Shenzhen, Nanshan District, Guangdong Province, China"}          


May I ask what is the problem? How to solve? Thank you!





Reply to discussion (solution)

Print_r ($result);
What can you see?

$result is an object, the output is wrong:
Catchable fatal error:object of class Result could not being converted to string in D:\WWW\wsd\trunk\src\server\api\getClien Ts.php on line 44

Object of class Result could not being converted to string objects cannot be converted to strings
And, of course, it can't be json.
Take a look at your Client class definition

It might not be worth it.

Coding problem, Json_encode () is only valid for Utf-8, when creating a database table, set the table encoding is utf-8, when connecting to the database also set the encoding on the line, similar to:
function Connect_host () {
$host = "127.0.0.1";
$name _db = "root";
$password _db = "root";
$name _TB = "wsd_db";
$con = mysql_connect ($host, $name _db, $password _db);

if (! $con) {
echo "Connect host failed". Mysql_error ();
Return
}
mysql_select_db ($name _tb, $con);
mysql_query ("SET NAMES UTF8"); #设置utf-8 encoding
return $con;
}

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.