Query Database Through Javascript / Php
I am having trouble trying to return results from a PHP script, back into my HTML / Jquery application. Currently, I am trying to connect to the server like so: function getDataba
Solution 1:
I would check out the following connection method.
http://api.jquery.com/jQuery.get/
This will solve all your issues and clean up your code
This is a sample of one of my old codes
$.ajax({
data: "id=" + value,
type: "POST",
url: "unlock.php",
success: function (data) {
if (data == 1 || data == true) {
var result = true;
} else {
var result = false;
}
}
});
Solution 2:
This is the simplest jQuery JavaScript I could get it down to (this uses the GET method):
functiongetDatabaseRows()
{
$.getJSON('myDomain.com/subDirectory/getRowCounts.php', function(data) {
console.log(data);
});
}
Documentation at http://api.jquery.com/jQuery.getJSON/
And some enhancements to your PHP using a foreach {}
and mysql_result()
, although you may want to look into PHP's mysqli
class for a more future-proof solution.
$rowCounts = array();
$dbhost = 'host';
$dbuser = 'host';
$dbpass = 'host';
$dbase = 'host';
$fields = array(
'MaxTID' => array('tab' => 'TransData', 'col' => 'TID'),
'MaxSID' => array('tab' => 'FullData', 'col' => 'SID'),
'MaxFID' => array('tab' => 'SalamanderData', 'col' => 'FID'),
'MaxOID' => array('tab' => 'OthersData', 'col' => 'OID')
);
$con = mysql_connect($dbhost, $dbuser, $dbpass) ordie('Error connecting to mysql');
mysql_select_db($dbase, $con) ordie(mysql_error());
foreach ($fieldsas$id => $info) {
$sql = "SELECT MAX({$info['col']}) FROM {$info['tab']}";
$result = mysql_query($sql, $con);
if (!$result) die('Could not query:' . mysql_error());
$rowCounts[$id] = mysql_result($result, 0);
}
echo json_encode($rowCounts);
mysql_close($con);
This code worked on a GoDaddy hosted site. For the record, this is my first Stack Overflow answer. Please advise :)
Post a Comment for "Query Database Through Javascript / Php"