i am working with JSON to send data to and from my server , but normally i work with sending one data at a time , now i want to :
retrieve all the rows from a table (mysql database) --> php put it in a JSON array + callback ---> javascript retrives it and display it out by looping through the data.
Here's my javascript(jQuery):
$.getJSON(domain_path + 'generate.php?table=' + tbname + '&callback=?', function(data) {
});
As you can see this has table= the table name. which is for php to know which table to extract the data from.
But for the php part im not sure what to use to produce a JSONP array.
<?php
//connects to database
include 'connect.php';
$tbname = mysql_real_escape_string($_GET['table']);
$callback = $_GET['callback'];
//some mysql commands
//after mysql commands
//i would use this to output data but this is only for one line of data.
$output = array('error'=>'0');
$out_string = json_encode($output);
echo $callback.'('.$out_string.');';
?>
Mysql table structure:
Table name : users
name , link , email
How can i get all the rows from users table which contians their name , link and email and out it into a JSON array.
And how would i display it out using javascript(jquery)?
Is it using the for
function in javascript
mysql_real_escape_string()
is useless for escaping an object name, like a table or column name. If you quote it with backticks`
in the query, and error out ifstrpos($_GET['table'], '`') !== FALSE
then you should be fairly safe. – DaveRandom Apr 3 '12 at 8:17