send json data from php

json, php

Solution

Try as this

$result=mysql_query($sql, $con);
$json = array();
while($row = mysql_fetch_array($result))     
 {
    $json[]= array(
       'name' => $row['name'],
     'password' => $row['password']
    );
}

$jsonstring = json_encode($json);
 echo $jsonstring;

And mysql is deprecated, when you can use mysqli or PDO

Problem

I am totally new to Php and i am trying to send json data from php to android.I have the following code in php to read value from data base: ``` <?php $con=mysql_connect("localhost","root",""); if(! $con) { die('Connection Failed'.mysql_error()); } mysql_select_db("registration",$con); $name="Adam";//$_POST["name"]; $password="charles";//$_POST["password"]; $sql="SELECT * FROM users WHERE name='$name'and password='$password'"; $result=mysql_query($sql, $con); while($row = mysqli_fetch_array($result)) { $details= array( 'name' => $row['name'], 'password' => $row['password'], ); array_push($json, $bus); } $jsonstring = json_encode($json); echo $jsonstring; mysql_close(); ?> ``` I am expecting the output to be something like this: ``` [{"name":"Adam","age":"25","surname":"charles"}] ``` If i am not wrong the JSON data. But this gives me error : ``` mysqli_fetch_array() expects parameter 1 to be mysqli_result, resource given in... ``` and also ``` Undefined variable: json in... ``` can somebody pleease tell me what might be the possible error

Original source