1

I am trying to fetch data from users table but PHP shows blank page and no error. Where am I wrong? I am newbie in 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);

?>

EDİT

I added var_dump($row); in while loop

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

?>

Now it shows,

> 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) "" }
Sloan Thrasher
  • 4,953
  • 3
  • 22
  • 40
user1937692
  • 35
  • 1
  • 9

4 Answers4

0

Here is how the json looks when formatted with Json viewer plugin, it appears to be wrongly formed

>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)""
}

Can you please post the var_dump($result) or print_r($result). It could be as others have suggested the table itself might be empty.

MSangha
  • 55
  • 1
  • 1
  • 5
0

I had a similar problem due to encoding.

This solved it for me.

In short: make sure encoding is correct.

Adding this at the top of your page should help.

header("Content-Type: text/html; charset=ISO-8859-1")
Community
  • 1
  • 1
JustCarty
  • 3,839
  • 5
  • 31
  • 51
0

I solved my problem it was about Turkish characters. There was some turkish characters like ü in my database. Then truncated my table and added again some english characters in my database. Now my problem is solved.

user1937692
  • 35
  • 1
  • 9
-3

Add this at start of script (as code, not in php.ini):

error_reporting(-1);
ini_set('display_errors', 'On');

Then if you still don't get anything, check mysql table with phpmyadmin. Could be empty.

Try adding this after $result = mysqli_query($con,$sql);

echo mysqli_errno($con) . ": " . mysqli_error($con). "\n";
TopReseller
  • 623
  • 4
  • 6