0

using php database connection i want to display data in json format which data are fatched from database(MySql),but i can't displaying in json format. http://takeyourtime.16mb.com/fatchData.php

$con = mysqli_connect($host, $username, $pwd, $db) or die('Unable to connect');
if (mysqli_connect_error($con))
{
  echo "Failed to Connect to Database ".mysqli_connect_error();
}

$name = $_POST['Query'];
$sql = "SELECT * FROM playerstb";
$query = mysqli_query($con,$sql);

if ($query)
{
  $rows = array();
  while ($r = mysql_fetch_assoc($query)) {
    $rows['root_name'] = $r;
  }
}

echo json_encode($rows);

mysqli_close($con);
Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
Ayon Dey
  • 1
  • 2
  • Display in JSON format? – Jay Blanchard Aug 02 '16 at 18:22
  • sure your not looking for print_r() ? - and json_encode() – kurt Aug 02 '16 at 18:23
  • Also, you overwrite `$rows['root_name']` each iteration so you will only ever have one row. – AbraCadaver Aug 02 '16 at 18:30
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 02 '16 at 19:08

3 Answers3

0

Just use json_encode. BTW, your script has an syntax error in the ending if block:

if($query){

    $rows = array();
    while($r = mysql_fetch_assoc($query)) {
        $rows['root_name'][] = $r; // probably must be an array
    }
    echo json_encode($rows);

}else{
    /* 
    This will show up when you have a query error
    nothing to do with the results found.
    I would consider changing the message below
    */
    echo('Not Found');
}
tyb
  • 201
  • 4
  • 13
CarlosCarucce
  • 3,420
  • 1
  • 28
  • 51
  • @AyonDey remove the extra `}` before the *echo* call – CarlosCarucce Aug 02 '16 at 18:35
  • @AyonDey I visited http://takeyourtime.16mb.com/fatchData.php, provided in the question. Its appears to be OK to me (Even filled with test values). Here's the output I am getting: `[{"0":"1","id":"1","1":"ayon","name":"ayon","2":"hii","massage":"hii"},{"0":"2","id":"2","1":"ankush","name":"ankush","2":"eeee","massage":"eeee"},{"0":"3","id":"3","1":"xxx","name":"xxx","2":"hiii","massage":"hiii"}]` – CarlosCarucce Aug 02 '16 at 18:47
0

inside your while loop you dont save all results you each one writen over the before one

you have to store it in array like note this ([])*

  while($r = mysql_fetch_assoc($query)) {
    $root_names[] = $r;
   }
  echo json_encode(['root_name'=>$root_names]);
0

You have to store first your result in array then after that create an array name your desire key ($array["name"])

$con=mysqli_connect($host,$username,$pwd,$db) or die('Unable to connect');
if(mysqli_connect_error($con))
{
  echo "Failed to Connect to Database ".mysqli_connect_error();
}
$name=$_POST['Query'];
$sql="SELECT * FROM playerstb";
$query=mysqli_query($con,$sql);
if($query)
{


$rows = array();
while($r = mysql_fetch_assoc($query)) {
 $rows[] = $r;
}
$data["data"]=$rows;
echo json_encode($data);
}

}else
{
 echo('Not Found ');
}
 mysqli_close($con);
?>
Paul
  • 149
  • 1
  • 13