Search code examples
mysqlmysql-error-1064

my sql fetch array and json


I have a function; if the function is working url = "ambildata.php?akhir=0"; is executed and then do

$query = "SELECT id_hijau, judul, deskripsi, jenis, x, y FROM hijau ORDER BY id_hijau DESC LIMIT 1";

after that

$data = mysql_query($query);

$json = '{"wilayah": {';
$json .= '"petak":[ ';
while($x = mysql_fetch_array($data)){
    $json .= '{';
    $json .= '"id":"'.$x["id_hijau"].'",
        "judul":"'.htmlspecialchars($x["judul"]).'",
        "deskripsi":"'.htmlspecialchars($x["deskripsi"]).'",
        "x":"'.$x["x"].'",
        "y":"'.$x["y"].'",
        "jenis":"'.$x["jenis"].'"

    },';
}
$json = substr($json,0,strlen($json)-1);
$json .= ']';

$json .= '}}';

echo $json;

but i have an error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\ta\web\ambildata.php on line 13 {"wilayah": {"petak":[]}}

please help me solve my problem...

thanks


Solution

  • The query produced an error in stead of results. As noted in the documentation for mysql_query, it will return BOOL false if there is an error. Use mysql_error to see what's going on.

    EDIT: What your error means is that you are passing in either TRUE of FALSE to mysql_fetch_array, which indicates that your $data is either TRUE of FALSE, and since $data = mysql_query($query);, that means mysql_query is returning TRUE or FALSE, and the only time it does that is when there is an error executing the query; it returns false. This means that the real issue is not with mysql_fetch_array, but with your query or elsewhere at the DB level.