-1

I'm trying to get data from a mysql table and send it via a php script as a json string so it later can be used by for example ios apps.

The code i have so far is:

<?php

$con = mysql_connect("xxx", "xxx", "xxx");

mysql_select_db("FreeSir_MarinaLaroverket") or die("Unable to select database");

$arr = array();

$rs = mysql_query("SELECT * FROM Nyheter");

while($obj = mysql_fetch_assoc($rs))
{
$arr[] = $obj;
}

echo json_encode($arr);

?>

But when i use the script i get the following:

[{"Index":"1","Title":null,"News":null,"Date":"11\/1"},{"Index":"2","Title":"Andra nyheten","News":null,"Date":"22\/2"}]

As you might see there, i have some null values that pops up there from nowhere. I've doublechecked that i have the correct values inserted and all, but it still just gives me null.

I would appreciate it if anyone of you could see what is making this code not giving me all the values i want.

Best Regards

FreeSirenety

4
  • 1
    have you tried doing a var_dump($arr) ? to see what is inside? Commented Mar 6, 2013 at 14:08
  • 1
    as an aside, PHP functions that start with mysql_ have been deprecated as of PHP 5.5.0. If you are in a position to do so, please consider updating your code to use the MySQLi or PDO extensions instead. Commented Mar 6, 2013 at 14:09
  • stackoverflow.com/a/15055993/1270996 Look here for how to convert your script from mysql_ to mysqli_. Commented Mar 6, 2013 at 14:10
  • what result you want ? Commented Mar 6, 2013 at 14:22

1 Answer 1

0

If your var_dump($arr) statement displays the correct data, so you probably have a problem with the JSON representation. Check the manual for json_encode(), and look for the $options parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.