从php发送json数据 [英] send json data from php

查看:155
本文介绍了从php发送json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Php完全陌生,我正在尝试将json数据从php发送到android.php中有以下代码可以从数据库中读取值:

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();
?>

我期望输出是这样的:

[{"name":"Adam","age":"25","surname":"charles"}]

如果我没有记错的JSON数据. 但这给了我错误:

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...

还有

Undefined variable: json in...

有人可以告诉我可能是什么错误

can somebody pleease tell me what might be the possible error

推荐答案

以此尝试

$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;

当您可以使用mysqli或PDO时,不建议使用mysql

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

这篇关于从php发送json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆