1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
function generateJSON() {
// Here we get the content of the requested entry
$id = escape($_GET['id']); // <== WARNING ! SQL INJECTION ! Please escape it !!
if (!isset($id))
return FALSE;
$result = mysql_query("SELECT * FROM produit WHERE produit_id=".$id." ORDER BY produit_nom LIMIT 1") or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC); // Here we have 1 row (LIMIT 1)
// This puts the result of mysql_query in a JSON output form
// Will be like {'column_name': 'column_value', 'column2_name': 'column2_value'}
print "{";
while ($key = key($row)) {
print "'" . $key . "': '". str_replace("\"", "\\\"", htmlspecialchars($row[$key])) ."'"; // Here, I escape "
if (next($row) !== FALSE) // The two = in !== are mandatory ! It's not a mistake ! (see PHP doc)
print ",";
}
print "}";
} |
Partager