Stuart_Westgate Posted May 9, 2013 Share Posted May 9, 2013 This is my script: I'm trying to save the contents of a database to json I've getting the database contents to echo out but can get it encoded into json. How can I do this or what am I doing wrong here? <?phpecho '<link rel="stylesheet" href="css/styles.css" />'; // Create connection //$connection = mysqli_connect("localhost","root","","finalprojectapp"); //Localhost Connection //Check Connection if(mysqli_connect_errno($connection)){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); }else{ echo "Success, Connection Achieved"; } $result = mysqli_query($connection,"SELECT * FROM member"); while($row = mysqli_fetch_array($result)) { echo "<br/><br/>ID: <p>" . $row['id'] . "</p> Phone Number: <p>" . $row["phoneNumber"] . "</p> First Name: <p>" . $row["firstName"] . "</p> Last Name: <p>" . $row["lastName"] . "</p> Profile Picture: <p>" . $row["profilePicture"] . "</p> Photo: <p>" . $row["photo"] . "</p> Video: <p>" . $row["video"] . "</p> Text: <p>" . $row["text"] . "</p> Call: <p>" . $row["call"] . "</p> Activity: <p>" . $row["activity"] . "</p> Latitude: <p>" . $row["latitude"] . "</p> Longitude: <p>" . $row ["longitude"] . "</p> Date: <p>" . $row["date"] . "</p> Time: <p>" . $row["time"] . "</p><br/>"; }$a = array($row["id"],$row['phoneNumber'],$row['firstName'], "\xc3\xa9");echo "Normal: ", json_encode($a), ""; mysqli_close($connection); Quote Link to comment https://forums.phpfreaks.com/topic/277852-how-can-i-save-the-my-database-contents-to-json/ Share on other sites More sharing options...
Barand Posted May 9, 2013 Share Posted May 9, 2013 what am I doing wrong here? You are not using the forums code tags, or the <> button Quote Link to comment https://forums.phpfreaks.com/topic/277852-how-can-i-save-the-my-database-contents-to-json/#findComment-1429378 Share on other sites More sharing options...
Jessica Posted May 10, 2013 Share Posted May 10, 2013 Well, what's the output of it? Quote Link to comment https://forums.phpfreaks.com/topic/277852-how-can-i-save-the-my-database-contents-to-json/#findComment-1429413 Share on other sites More sharing options...
Stuart_Westgate Posted May 12, 2013 Author Share Posted May 12, 2013 Sorry for not responding straight away. I've just got back from a camping trip. Also sorry for not wrapping my code in <>. The most important part of the code I need working isn't working: It's this section: $a = array($row["id"],$row['phoneNumber'],$row['firstName'], "\xc3\xa9"); echo "Normal: ", json_encode($a), ""; Which is returning this: Normal: [null,null,null,"\u00e9"] I want it to return the values stored in my mySQL database and not null but it doesn't seem to be working. Any ideas Quote Link to comment https://forums.phpfreaks.com/topic/277852-how-can-i-save-the-my-database-contents-to-json/#findComment-1429743 Share on other sites More sharing options...
Barand Posted May 12, 2013 Share Posted May 12, 2013 By the time you you create the array $a you have read all the data returned so the $row is empty. Create $a inside the loop Quote Link to comment https://forums.phpfreaks.com/topic/277852-how-can-i-save-the-my-database-contents-to-json/#findComment-1429746 Share on other sites More sharing options...
Stuart_Westgate Posted May 14, 2013 Author Share Posted May 14, 2013 Thanks that worked perfectly Quote Link to comment https://forums.phpfreaks.com/topic/277852-how-can-i-save-the-my-database-contents-to-json/#findComment-1429982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.