PHP error can not retrieve LIKE statements from android application
I am trying to search up the database with an android application, but when I search for a string element with the SQL syntaxes 'LIKE',
such as:
SELECT * FROM `test123` WHERE (`name` LIKE '%A%')
it would give me an error:
A JSONArray text must start with '[' at character 1 of ....
but if I search for:
SELECT * FROM `test123`
it'll work fine.
<?php
mysql_connect("website.com","root","password");
mysql_select_db("test123");
$q=开发者_Python百科mysql_query($_REQUEST['sql']);
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
$output = array();
while ($e = mysql_fetch_assoc($q)) {
$output[] = $e;
}
The problem may simply be that if there are no results, $output
is never defined. When you then try to json_encode($output)
, PHP throws a warning about $output
being undefined, which leads to final output like:
PHP Notice: Undefined variable: output in ...
null
Which is invalid JSON. Even if your warnings are suppressed, the output will just be null
, which is not a JSON array. If your client expects an array, then it will be disappointed by null
.
Always initialize your variables.
It should be just
"SELECT * FROM test123 WHERE name LIKE '%A%'"
i think this can be work fine SELECT * FROM test123
(name
) WHERE (name
LIKE '%A%')
When I user php json_encode
it always returns a JSONObject
, not a JSONArray
. Try printing your raw result to Logcat and see what form it's in.
精彩评论