A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearJSON encode MySQL results - page 1
User InfoPosts
JSON encode MySQL results#1
How do I use the json_encode() function with MySQL query results? Do I need to iterate through the rows or can I just apply it to the entire results object?

posted date: 2008-12-20 11:09:00


Re: JSON encode MySQL results#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-20 11:09:01


Re: JSON encode MySQL results#3
http://www.php.net/mysql_query says "mysql_query() returns a resource".http://www.php.net/json_encode says it can encode any value "except a resource".You need to iterate through and collect the database results in an array, then json_encode the array.

posted date: 2008-12-20 11:41:00


Re: JSON encode MySQL results#4
$sth = mysqli_query("SELECT ...");$rows = array();while($r = mysqli_fetch_assoc($sth)) { $rows[] = $r;}print json_encode($rows);The function json_encode needs PHP >= 5.2 and the php-json package - as mentioned hereNOTE: mysql is deprecated as of PHP 5.5.0, use mysqli extension instead http://php.net/manual/en/migration55.deprecated.php.

posted date: 2008-12-20 11:48:00


Re: JSON encode MySQL results#5
mysql_query does not return a result set. that's what mysql_fetch* is for.

posted date: 2008-12-20 17:30:00


Re: JSON encode MySQL results#6
Um... yeah... that's what goes in the iterating, between mysql_query and json_encode. Good call, Watson.

posted date: 2009-01-16 20:27:00


Re: JSON encode MySQL results#7
The above will not work, in my experience, before you name the root-element in the array to something, I have not been able to access anything in the final json before that.$sth = mysql_query("SELECT ...");$rows = array();while($r = mysql_fetch_assoc($sth)) { $rows['root_name'] = $r;}print json_encode($rows);That should do the trick!Pär

posted date: 2009-11-15 05:19:00


Re: JSON encode MySQL results#8
Try this, this will create your object properly $result = mysql_query("SELECT ..."); $rows = array(); while($r = mysql_fetch_assoc($result)) { $rows['object_name'][] = $r; } print json_encode($rows);

posted date: 2011-01-22 12:19:00


Re: JSON encode MySQL results#9
I would as advise as you to mention that during the select query to use AS to rename the columns to something for public such as SELECT blog_title as title, this is cleaner and the public do not know what the exact columns are from the database.

posted date: 2011-02-05 09:04:00


Re: JSON encode MySQL results#10
Thanks this helped me a lot. My code:$sqldata = mysql_query("SELECT * FROM `$table`");$rows = array();while($r = mysql_fetch_assoc($sqldata)) { $rows[] = $r;}echo json_encode($rows);

posted date: 2011-05-24 02:16:00


Re: JSON encode MySQL results#11
This code erroneously encodes all numeric values as strings. For example, a mySQL numeric field called score would have a JSON value of "12" instead of 12 (notice the quotes).

posted date: 2011-09-25 10:48:00


Re: JSON encode MySQL results#12
@RobertPitt, security based on concealing names of your columns is security by obscurity!

posted date: 2011-12-02 13:10:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.