0

I'm in the process of building an iOS app that uses a webservice for data. The webservice consists of PHP, MySQL database.

I've successfully managed to JSON encode the data returned, but the code I am using only seems to encode 1 row.

I wanted to get some advice on how to encode multiple rows?

Thanks.

<?php
$username = "root";
$password = "*******";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");

//print "Connected to MySQL<br>";

$selected = mysql_select_db("newtest",$dbh)
or die("Could not select first_test");

$query = "SELECT * FROM MyGuests ";
$result=mysql_query($query);

echo json_encode(mysql_fetch_assoc($result));

?>
4

1 Answer 1

2
$query = "SELECT * FROM MyGuests ";
$result=mysql_query($query);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
  $rows[] = $r;
}
print_r json_encode($rows);
Sign up to request clarification or add additional context in comments.

2 Comments

You're mixing mysql_ and mysqli_ functions. That will not work.
Hi, thanks for this. I've implemented this change, and previously when accessing the URL for this query on a browser it displayed the single row as JSON, but not the page shows blank. Have I done something wrong or is this expected? Sorry for the noob questions!

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.