Jump to content

json_encode mysql data


borden0108

Recommended Posts

Hi All

 

I got some help to write this code and it is not working because i get the error ( ! ) Parse error: syntax error, unexpected '{' in C:\wamp\www\blackrain\Resources\json-gen.php on line 14

 

Here is the php code

 

<?php
error_reporting(-1);
ini_set('display_errors',1);

$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
mysql_select_db("php");
$arr = array();
$rs =mysql_query("SELECT `id`, `title`, `author`, `date`, `imageUrl`, `text` FROM `items`");

while($obj = mysql_fetch_array($rs,MYSQL_ASSOC) {
$arr[0] =  $obj['id'];
$arr[1] =  $obj['title'];
$arr[2] =  $obj['author'];
$arr[3] =  $obj['date'];
$arr[4] =  $obj['imageUrl'];
$arr[5] =  $obj['text'];
}
//numbering the array indexes, will only return 5 array indexes EVER.  All indexes will be over written on each loop.
echo json_encode($arr);
?>

 

the sql data is below please put in a db named php and a table named items

 

-- phpMyAdmin SQL Dump

-- version 3.3.9

-- http://www.phpmyadmin.net

--

-- Host: localhost

-- Generation Time: Sep 10, 2011 at 11:49 AM

-- Server version: 5.5.8

-- PHP Version: 5.3.5

 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

 

 

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

/*!40101 SET NAMES utf8 */;

 

--

-- Database: `php`

--

 

-- --------------------------------------------------------

 

--

-- Table structure for table `items`

--

 

CREATE TABLE IF NOT EXISTS `items` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `title` text NOT NULL,

  `author` text NOT NULL,

  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  `imageUrl` text NOT NULL,

  `text` text NOT NULL,

  `catagory` text NOT NULL,

  KEY `id` (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=25 ;

 

--

-- Dumping data for table `items`

--

 

INSERT INTO `items` (`id`, `title`, `author`, `date`, `imageUrl`, `text`, `catagory`) VALUES

(2, 'some random title', 'me', '0000-00-00 00:00:00', 'http://forums.mysql.com/read.php?45,55300,56787#msg-56787', 'fvsdhlihldj sdfjvhduolh dvjichis', '0'),

(4, '', '', '0000-00-00 00:00:00', '', '', '0'),

(5, 'yet another trial', 'me', '0000-00-00 00:00:00', 'http://www.gaj-it.com/wp-content/uploads/apple5.jpg', ' nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn', '0'),

(6, 'yet another trial', 'me', '0000-00-00 00:00:00', 'http://www.gaj-it.com/wp-content/uploads/apple5.jpg', ' nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnf nnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnn', '0'),

(7, '', '', '0000-00-00 00:00:00', '', ' ', '0'),

(8, '', '', '0000-00-00 00:00:00', '', '', '0'),

(9, 'n', '', '0000-00-00 00:00:00', '', '', '0'),

(10, 'n', 'me', '0000-00-00 00:00:00', '0000', 'me', '0'),

(11, 'n', 'me', '0000-00-00 00:00:00', '0000', 'me1', '0'),

(12, 'M', 'Me', '0000-00-00 00:00:00', 'url', 'jdoiheouhfviuehvuiohbeuiofhvuiheiufhvuiehuifvheiouhfvoiuhrovheovhuhfvbuohouifhbouehouhbveiouhfbvebhsousuobv ousiuodfbvibfuibvousdubfdfbfbuvbbfdbufvbjjbfvdjfjfvdjfbfbfdfbbfbbfkbjkbjbfdjbjfkbjkbjbjnbjnnbdnbnjjnbdnjkbfdjknbdnjkbdnjkbdnjkbfdnjkbdnjkbfdnjkbnjkbfdnjkgbnjkgbnjknknknbngbbbd', '0'),

(13, 'M', 'Me', '0000-00-00 00:00:00', 'url', 'jdoiheouhfviuehvuiohbeuiofhvuiheiufhvuiehuifvheiouhfvoiuhrovheovhuhfvbuohouifhbouehouhbveiouhfbvebh0pvipjfvoejnv oaiehnfvolijeaofvihn olan', '0'),

(14, 'M', 'Me', '0000-00-00 00:00:00', 'url', 'jdoiheouhfviuejnjfnfviejnfvienfivnloidfhnvvihn olan', '0'),

(15, 'hhikuh', 'h', '0000-00-00 00:00:00', 'j', ' uhuhiuhuhuiohuoiho', '0'),

(16, '', '', '0000-00-00 00:00:00', '', '', '0'),

(17, 'brenton', 'me', '0000-00-00 00:00:00', 'http://www.gaj-it.com/wp-content/uploads/apple5.jpg', 'hi all ', '0'),

(18, '', '', '0000-00-00 00:00:00', '', '', '0'),

(19, 'something', 'me', '0000-00-00 00:00:00', 'http://www.gaj-it.com/wp-content/uploads/apple5.jpg', 'ihfdvuikhavh\r\n ', '0'),

(20, '', '', '0000-00-00 00:00:00', '', '', '0'),

(21, '', '', '0000-00-00 00:00:00', '', '', '0'),

(22, '', '', '0000-00-00 00:00:00', '', '', '0'),

(23, 'cd', 'fg', '0000-00-00 00:00:00', 'srf', 'dfsf ', '0'),

(24, 'cd', 'fg', '0000-00-00 00:00:00', 'srf', 'dfsf ', 'audi');

 

thanks matt

 

Link to comment
Share on other sites

got it

 

could you also help with finding a way to not number the indexes becuse they are written over every time and i only end up with the last record

 

 

$arr = array();

$rs =mysql_query("SELECT `id`, `title`, `author`, `date`, `imageUrl`, `text`, `catagory` FROM `items`);

 

while ($obj = mysql_fetch_array($rs, MYSQL_ASSOC)) {

$arr[0] =  $obj['id'];

$arr[1] =  $obj['title'];

$arr[2] =  $obj['author'];

$arr[3] =  $obj['date'];

$arr[4] =  $obj['imageUrl'];

$arr[5] =  $obj['text'];

}

 

Link to comment
Share on other sites

You could make it a multidimensional array, and just add to it on each iteration of the while loop.

 

$arr = array();
while( $obj = mysql_fetch_row($rs) ) {  // mysql_fetch_row() will return the results as an enumerated array
     $arr[] = $obj;  // store the $obj array as an element of the $arr array 
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.