Reputation: 11
Guys i have a problem actually my php code is correct but i dont know why it display undefined into my client side script. Could somebody help me
here is my api.php this is where i put my php
<?php
$dbcon = mysqli_connect("localhost", "root", "", "orms") or die("Server not available" . mysql_error());
$data = array();
$result = mysqli_query($dbcon,"SELECT * FROM cottages") or die(mysqli_error()); //query
//$array = mysqli_fetch_row($result);
$data = array();
while($row = mysqli_fetch_assoc($result))
{
$data[] = $row;
}
echo json_encode( $data ) //fetch result
?>
here is my client.php code
<?php
$dbcon = mysqli_connect("localhost", "root", "", "orms") or die("Server not available" . mysql_error());
?>
<html>
<head>
<script language="javascript" type="text/javascript" src="jquery.js"> </script>
</head>
<body>
<h2> Client example </h2>
<h3>Output: </h3>
<div id="output"></div>
<script id="source" language="javascript" type="text/javascript">
$(function ()
{
$.ajax({
url: 'api.php', data: "POST", dataType: 'json', success: function(rows)
{
for (var i in rows)
{
var row = rows[i];
var cot_id = row[0];
var image = row[1];
$('#output').append("<b>cottage: </b>"+cot_id+"<b> image: </b>"+image)
.append("<hr />");
}
}
});
});
</script>
</body>
</html>
Thanks for your help in advance..
Upvotes: 1
Views: 2015
Reputation: 780974
You're using mysqli_fetch_assoc
, so the row will be an associative array, which turns into a Javascript object. But in the Javascript code you're accessing row
as if it's an array, not an object. You need to use the column names:
var cot_id = row.cot_id;
var image = row.image;
(I'm just guessing the column names, because you used SELECT *
so I can't see the actual names in your table.)
Upvotes: 1