I can probably give you basic help on this.
Edit: had to modify your first 2 echo's for json_encode, sorry.
I have modified your file just a bit to be able to handle json output.
<?php
// This file I have named it getdata.php
// And you will see why
include("connect.php");
//Query of facebook database
$facebook = mysql_query("SELECT * FROM facebook")
or die(mysql_error());
//Output results
if(!$facebook)
{
mysql_close();
echo json_encode("There was an error running the query: " . mysql_error());
}
elseif(!mysql_num_rows($facebook))
{
mysql_close();
echo json_encode("No results returned");
}
else
{
$header = false;
$output_string = "";
$output_string .= "<table border='1'>\n";
while($row = mysql_fetch_assoc($facebook))
{
if(!$header)
{
$output_string .= "<tr>\n";
foreach($row as $header => $value)
{
$output_string .= "<th>{$header}</th>\n";
}
$output_string .= "</tr>\n";
}
$output_string .= "<tr>\n";
foreach($row as $value)
{
$output_string .= "<th>{$value}</th>\n";
}
$output_string .= "</tr>\n";
}
$output_string .= "</table>\n";
}
mysql_close();
// This echo for jquery
echo json_encode($output_string);
?>
The html file, (php for me) I have named it display.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Display Page</title>
</head>
<body>
<button type="button" name="getdata" id="getdata">Get Data.</button>
<div id="result_table">
</div>
<script type="text/javascript" language="javascript">
$('#getdata').click(function(){
$.ajax({
url: "getdata.php",
type:'POST',
dataType: 'json',
success: function(output_string){
$("#result_table").append(output_string);
} // End of success function of ajax form
}); // End of ajax call
});
</script>
</body>
</html>
Since I have no idea what your page looks like, I just made a simple button, that when clicked, it will look inside the DB, grab data, and return it, placing it inside the div.
Now I have not tested this, but this is how it should be looking like.
If you get any errors let me know, I will help out.