我正在尝试从用户表中获取数据,但PHP显示空白页并且没有错误。我哪里错了?我是PHP的新手。
<?php
$host ="localhost";
$database="xyz"; //I am sure that here is true
$username="xyz"; //I am sure that here is true
$password="xyz"; //I am sure that here is true
$sql = "select * from users;";
$con = mysqli_connect($host,$username,$password,$database);
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result)){
array_push($response,array("name" => $row[0],"email" => $row[1],"contact" => $row[2],"password" => $row[3]));
}
echo json_encode(array("server_response" =>$response));
mysqli_close($con);
?>
修改
我在中添加了var_dump($row);
,而循环
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$host ="localhost";
$database="gurkanc1_sample";
$username="gurkanc1_sample";
$password="Sample123";
$sql = "select * from users;";
$con = mysqli_connect($host,$username,$password,$database);
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result)){
array_push($response,array("name" => $row[0],"email" => $row[1],"contact" => $row[2],"password" => $row[3]));
var_dump($row);
}
echo json_encode(array("server_response" =>$response));
mysqli_close($con);
?>
现在显示,
> array(8) { [0]=> string(3) "asd" ["name"]=> string(3) "asd" [1]=>
> string(3) "asd" ["email"]=> string(3) "asd" [2]=> string(2) "aa"
> ["contact"]=> string(2) "aa" [3]=> string(2) "aa" ["password"]=>
> string(2) "aa" } array(8) { [0]=> string(13) "Merhaba D�nya"
> ["name"]=> string(13) "Merhaba D�nya" [1]=> string(7) "Merhaba"
> ["email"]=> string(7) "Merhaba" [2]=> string(7) "Merhaba"
> ["contact"]=> string(7) "Merhaba" [3]=> string(7) "Merhaba"
> ["password"]=> string(7) "Merhaba" } array(8) { [0]=> string(0) ""
> ["name"]=> string(0) "" [1]=> string(0) "" ["email"]=> string(0) ""
> [2]=> string(0) "" ["contact"]=> string(0) "" [3]=> string(0) ""
> ["password"]=> string(0) "" } array(8) { [0]=> string(2) "ss"
> ["name"]=> string(2) "ss" [1]=> string(2) "ss" ["email"]=> string(2)
> "ss" [2]=> string(2) "ss" ["contact"]=> string(2) "ss" [3]=> string(1)
> "s" ["password"]=> string(1) "s" } array(8) { [0]=> string(4) "axac"
> ["name"]=> string(4) "axac" [1]=> string(3) "qdq" ["email"]=>
> string(3) "qdq" [2]=> string(4) "egeg" ["contact"]=> string(4) "egeg"
> [3]=> string(3) "wff" ["password"]=> string(3) "wff" } array(8) {
> [0]=> string(7) "merhaba" ["name"]=> string(7) "merhaba" [1]=>
> string(7) "nerbaba" ["email"]=> string(7) "nerbaba" [2]=> string(3)
> "cii" ["contact"]=> string(3) "cii" [3]=> string(4) "asdf"
> ["password"]=> string(4) "asdf" } array(8) { [0]=> string(1) "g"
> ["name"]=> string(1) "g" [1]=> string(1) "e" ["email"]=> string(1) "e"
> [2]=> string(2) "er" ["contact"]=> string(2) "er" [3]=> string(1) "r"
> ["password"]=> string(1) "r" } array(8) { [0]=> string(2) "sd"
> ["name"]=> string(2) "sd" [1]=> string(2) "sd" ["email"]=> string(2)
> "sd" [2]=> string(3) "dsd" ["contact"]=> string(3) "dsd" [3]=>
> string(0) "" ["password"]=> string(0) "" }
答案 0 :(得分:0)
以下是json在使用Json viewer插件格式化时的外观,它似乎是错误形成的
>array(8){
[0]=>string(3)"asd"["name"]=>string(3)"asd"[1]=>>string(3)"asd"["email"]=>string(3)"asd"[2]=>string(2)"aa">["contact"]=>string(2)"aa"[3]=>string(2)"aa"["password"]=>>string(2)"aa"
}array(8){
[0]=>string(13)"Merhaba D�nya">["name"]=>string(13)"Merhaba D�nya"[1]=>string(7)"Merhaba">["email"]=>string(7)"Merhaba"[2]=>string(7)"Merhaba">["contact"]=>string(7)"Merhaba"[3]=>string(7)"Merhaba">["password"]=>string(7)"Merhaba"
}array(8){
[0]=>string(0)"">["name"]=>string(0)""[1]=>string(0)""["email"]=>string(0)"">[2]=>string(0)""["contact"]=>string(0)""[3]=>string(0)"">["password"]=>string(0)""
}array(8){
[0]=>string(2)"ss">["name"]=>string(2)"ss"[1]=>string(2)"ss"["email"]=>string(2)>"ss"[2]=>string(2)"ss"["contact"]=>string(2)"ss"[3]=>string(1)>"s"["password"]=>string(1)"s"
}array(8){
[0]=>string(4)"axac">["name"]=>string(4)"axac"[1]=>string(3)"qdq"["email"]=>>string(3)"qdq"[2]=>string(4)"egeg"["contact"]=>string(4)"egeg">[3]=>string(3)"wff"["password"]=>string(3)"wff"
}array(8){
>[0]=>string(7)"merhaba"["name"]=>string(7)"merhaba"[1]=>>string(7)"nerbaba"["email"]=>string(7)"nerbaba"[2]=>string(3)>"cii"["contact"]=>string(3)"cii"[3]=>string(4)"asdf">["password"]=>string(4)"asdf"
}array(8){
[0]=>string(1)"g">["name"]=>string(1)"g"[1]=>string(1)"e"["email"]=>string(1)"e">[2]=>string(2)"er"["contact"]=>string(2)"er"[3]=>string(1)"r">["password"]=>string(1)"r"
}array(8){
[0]=>string(2)"sd">["name"]=>string(2)"sd"[1]=>string(2)"sd"["email"]=>string(2)>"sd"[2]=>string(3)"dsd"["contact"]=>string(3)"dsd"[3]=>>string(0)""["password"]=>string(0)""
}
您可以发布var_dump($ result)或print_r($ result)。可能正如其他人所暗示的那样,表本身可能是空的。
答案 1 :(得分:0)
由于编码,我遇到了类似的问题。
This为我解决了这个问题。
简而言之:确保编码正确。
在页面顶部添加此内容应该有所帮助。
header("Content-Type: text/html; charset=ISO-8859-1")
答案 2 :(得分:0)
我解决了我的问题,这是关于土耳其人的角色。在我的数据库中有一些土耳其字符,如ü。然后截断我的表并在我的数据库中再添加一些英文字符。现在我的问题解决了。
答案 3 :(得分:-3)
在脚本开头添加它(作为代码,而不是在php.ini中):
error_reporting(-1);
ini_set('display_errors', 'On');
然后如果你还没有得到任何东西,请用phpmyadmin检查mysql表。可能是空的。
尝试在$ result = mysqli_query($ con,$ sql);
之后添加它echo mysqli_errno($con) . ": " . mysqli_error($con). "\n";