Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?
i have a javascript which does a XMLHttp request to a php file, which in turn queries a mysql table and returns a JSON array... but somehow i cannot access individual elements of the array. here is the javascript:
<script type="text/javascript">
function show(){
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange= function () {
if(xmlhttp.readyState==4 && xmlhttp.status==200){
var response= new Array();
response=xmlhttp.response;
alert(response);
}
}
xmlhttp.open('GET','test.php', true);
xmlhttp.send();
}
</script>
and this is the php script:
mysql_select_db('testpolaroid') or die ('Unable to select database!');
$query = 'SELECT * FROM images';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
if(mysql_num_rows($result) > 0)
{
$row = mysql_fetch_assoc($result);
echo json_encode($json);
}
the response that i get is :
{"imageid":"11","location":"11.jpg"}
mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. – Madara Uchiha Jan 19 at 11:37json array in javascript
seems a little redundant as json meansjavaScript object notation
. – NomikOS Jan 19 at 11:44how to read a json string in javascript
insteadhow to use json array in javascript
. Peace. – NomikOS Jan 19 at 12:04