I need to output data for javascript (array of array) from MySQL
I am using this code, which works fine except that REPLACE function
I need to modify all the text that goes into JS array’s inverted comas ‘ — ‘ as per JS format. And can be used for all the fields in 1 go.
I need a replace function that will escape all data, i.e. \ -> \\ ; ' -> \' ; newline -> \n
$sth=$dbh->prepare('
SELECT GROUP_CONCAT(
"\n[\'",
CONCAT_WS("\',\'", d6id, REPLACE(d6name,"\'","\\\\\'"), d6date),
"\']"
)
FROM d6lastdate
');
$sth->execute();($json)=$sth->fetchrow_array();
Output
['0000000001','CA\'T','2011-09-26'],
['0000000002','CWE','2011-09-23'],
['0000000003','MAT','0000-00-00'],
The best way is probably to do this outside of MySQL.
encode_jsonis provided by JSON::XS.