<script type="text/javascript">
$(document).ready(function () {
var query = "SELECT * FROM tbl_users WHERE user_id = 3";
$.getJSON("query_executor.php", { query: query}, function(data){
alert("user_name: " + data[0].user_name);
});
});
</script>
O
var html = $.ajax({
type: "GET",
url: "php_ajax/query_executor.php",
data: "query=" + query,
async: false
}).responseText;
var myObject = eval('(' + html + ')');
alert(var_dump_rck(myObject[0]));
El PHP
<?php
include("db.php");
$query = $_REQUEST['query'];
$result = mysql_query($query) or trigger_error(mysql_error());
$jsonArr = array();
while($row = mysql_fetch_object($result))
{
$jsonArr[] = $row;
}
echo json_encode($jsonArr);
?>
El Json Generado
[
{"user_id":"1","user_name":"pepe","user_fecha_nacimiento":"1986-09-01"},
{"user_id":"2","user_name":"juan","user_fecha_nacimiento":"1988-12-05"},
{"user_id":"3","user_name":"Ambrosio Romero","user_fecha_nacimiento":"1984-05-12"}
]
OJO con los acentos y eñes
ResponderEliminarwhile ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row = array_map("utf8_encode", $row);
$jsonArr[] = $row;
}