php 从php发送json数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15281707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
send json data from php
提问by Raj
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 完全陌生,我正在尝试将 json 数据从 php 发送到 android。我在 php 中有以下代码来从数据库中读取值:
<?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 :
如果我没有弄错 JSON 数据。但这给了我错误:
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
有人可以告诉我可能的错误是什么吗
回答by Sam
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
并且 mysql 已弃用,何时可以使用 mysqli 或 PDO
回答by Sixthpoint
In addition to the changes suggested by sam and pitchinnate, also consider adding php header to set the content type to json
除了sam和pitchinnate建议的改动之外,还可以考虑添加php header,将内容类型设置为json
header('Content-type: application/json');
That is if you are using android to request the json information remotely
那就是如果你使用android远程请求json信息
回答by Pitchinnate
You are using mysql_querywith mysqli_fetch_array. You need to use mysql_fetch_array.
您正在使用mysql_query与mysqli_fetch_array. 您需要使用mysql_fetch_array.
However mysql_* functions shouldn't be used anymore.
但是,不应再使用 mysql_* 函数。
Also another error:
还有一个错误:
array_push($json, $bus); // i believe $bus should be replaced with $details
回答by Pitchinnate
I think you should check the content of you variable $result (var_dump()); In fact, if result is not defined, your program will not enter the loop and $json won't be defined.
我认为您应该检查变量 $result (var_dump()); 的内容。事实上,如果没有定义result,你的程序就不会进入循环,也不会定义$json。
回答by Ignacio Quintero
Undefined variable: json is caused because you are trying to do array_push($json, $bus) but $json doesnt exist.
未定义变量: json 是因为您尝试执行 array_push($json, $bus) 但 $json 不存在而引起的。
you should declare it first
你应该先声明
$json = Array();
The mysql error is most likely caused because you are mixing mysql and mysqli modules.
mysql 错误很可能是因为您混合使用了 mysql 和 mysqli 模块。
Use mysqli only.
仅使用 mysqli。
$con=mysqli_connect("localhost","root","");
mysql_select_db("registration",$con);
...
$result=mysql_query($sql, $con);
Also it is generally a bad idea to store passwords in plain text. Consider using a hash function at least.
此外,以纯文本形式存储密码通常是一个坏主意。至少考虑使用散列函数。

