-3

Where the code is wrong its giving me an error on mysql_assoc;

if($_SERVER['REQUEST_METHOD'] == "POST"){
$sql=mysql_query("SELECT * FROM users");

$query = mysql_query($sql);
$json = array(); // create empty array
$i = 0; // start a counter
while($result=mysql_fetch_assoc($query)){ 
    $json[$i]['name'] = $result['name'];
    $json[$i]['email'] = $result['email'];
    $i++; 
}
if(count($json)>0){ 
}else{
$json = array( "msg" => "No infomations Found");
}
header('Content-type: application/json');


}

this an error after running the service;

<br />
<b>Warning</b>:  mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\satyam\services\select.php</b> on line <b>11</b><br />
sam
  • 21
  • 2
  • 1
    All the `mysql_*` functions can fail. You should check for errors. You should also not be using the `mysql` extension. It is very old and not supported anymore. Look into [MySQLi](http://php.net/mysqli) or [PDO](http://php.net/pdo)... further more, why are you calling `mysql_query` twice? it makes no sense. – Sverri M. Olsen Aug 12 '16 at 22:49

1 Answers1

0

Please search the error you get before posting a question. We get this question on the site every day.

Anyway, you get that error because your query failed, so mysql_fetch_assoc has nothing to fetch. Do:

$query = mysql_query($sql);
if(!$query) die (mysql_error());

It will show you the problem with your query. Only below that line can you assume that the query succeeded and proceed to fetch results.

BeetleJuice
  • 37,029
  • 18
  • 91
  • 152