php - convert mysqli result to json -
I have a mysqli query that I need to format Jason for mobile application
I have managed to create an XML document for query results, although I am looking for some more light things. (See below for my current XML code)
Any help or information a lot appreciates!
$ mysql = new mysqli (DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die ('There was a problem connecting to database'); $ Stmt = $ mysql- & gt; Prepare (Select the DISTINCT title, separated by the sections titled 'ASC); $ Stmt- & gt; Executed (); $ Stmt- & gt; Bind_result ($ title); // Create XML format $ doc = new DomDocument ('1.0'); // Make Root Root $ root = $ doc- & gt; Create Element ('xml'); $ Root = $ doc- & gt; Append child ($ root); // Add a node for each line ($ line = $ stmt-> fetch ()): $ occ = $ doc- & gt; Create Element ('Data'); $ Occs = $ root- & gt; Append child ($ occ); $ Child = $ doc- & gt; Create Element ('Section'); $ Child = $ occ- & gt; Attachment Children ($ Child); $ Value = $ doc- & gt; CreateTextNode ($ title); $ Value = $ child- & gt; Append Chilled ($ value); Endwhile; $ Xml_string = $ doc- & gt; Save xml (); Header ('content-type: application / xml; charset = ISO-885 9 -1'); // output xml jQuery ready echo $ xml_string;
$ mysqli = new mysqli ('localhost', 'user', 'Password', 'myDatabaseName'); $ MyArray = array (); If ($ result = $ mysqli- & gt; query ("SELECT * FROM1")) {while ($ row = $ result-> fetch_array (MYSQL_ASSOC)) {$ myArray [] = $ row; } Echo json_encode ($ myArray); } $ Result-> Close (); $ Mysqli- & gt; near ();
-
$ line = $ result-> Fetch_array (MYSQL_ASSOC)
-
$ myArray [] = $ line
Output like this:
< {"Id": "31", "name": "pruduct_name1", "price": "98"}, {"id": "30", "name": "pruduct_name2", " Price ":" 23 "}]
Comments
Post a Comment