i’m trying for days to get this working.. but i still fail.
<?php
$province = $_POST['test'];
$dbhost = 'localhost';
$dbname = 'news';
$dblogin = 'root';
$dbpass = '';
function dbConnect() {
global $dbhost;
global $dbname;
global $dblogin;
global $dbpw;
$db = mysql_connect($dbhost, $dblogin, $dbpw) or die("could not
connect to database: ".mysql_error());
mysql_select_db($dbname) or die("could not select database");
return $db;
}
function dbClose($db) {
mysql_close($db);
}
// basis code ^^
$db = dbConnect();
$query = "SELECT title FROM news_google_us";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo json_encode($row);
;}
?>
i get this as response:
{"title":"Obama and GOP in Deal on Tax Cuts - New York Times"}{"title":"WikiLeaks Founder Warns About More Dispatches - New York Times"}{"title":"S. Korea to make islands near N. Korea fortresses - Jerusalem Post"}{"title":"9th Circuit judges explore narrow routes to reinstate gay marriage - Los Angeles Times"}{"title":"Killing of Iranian Scientist Overshadows 6-Nation Nuclear Talks - BusinessWeek"}{"title":"Schwarzenegger proposal rebuffed - San Jose Mercury News"}{"title":"WikiLeaks list of 'critical' sites: Is it a 'menu for terrorists'? - Christian Science Monitor"}{"title":"Big moments in the Concorde's history - BusinessWeek"}{"title":"Citigroup Stake Is Sold for About $10.5 Billion as US Unwinds Investment - Bloomberg"}{"title":"Jets Vs. Patriots: New England In Full-On Kobra Kai Mode...No Mercy - SB Nation"}{"title":"Obama, GOP cut deal to extend tax cuts - USA Today"}
I use Jquery to get the data:
$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}});
$.getJSON('mysql-query.php', function(data) {
alert(data);
});
I’ve tried a stripped version
<?
$dsn = "mysql:host=localhost;dbname=news";
$username = "root";
$password = "";
$pdo = new PDO($dsn, $username, $password);
$rows = array();
if(isset($_GET['descrtiption'])) {
$stmt = $pdo->prepare("SELECT description FROM news_BBC_sport WHERE name = ? ORDER BY description");
// $stmt = $pdo->prepare("SELECT variety FROM fruit WHERE name = ? ORDER BY variety");
$stmt->execute(array($_GET['descrtiption']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r(phpinfo());
echo "</pre>";
}
echo json_encode($rows);
?>
tried this one
I’ve tried a stripped version
<?
$dsn = "mysql:host=localhost;dbname=news";
$username = "root";
$password = "";
$pdo = new PDO($dsn, $username, $password);
$rows = array();
if(isset($_GET['descrtiption'])) {
$stmt = $pdo->prepare("SELECT description FROM news_BBC_sport WHERE name = ? ORDER BY description");
// $stmt = $pdo->prepare("SELECT variety FROM fruit WHERE name = ? ORDER BY variety");
$stmt->execute(array($_GET['descrtiption']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r(phpinfo());
echo "</pre>";
}
echo json_encode($rows);
?>
tried this one
http://www.phpclasses.org/package/4369-PHP-Generate-JSON-representation-from-MySQL-queries.html
I’m using firebug to check what’s wrong. I get this:
parse error
syntax error JSON parse
(shows json data)
I’ve tried like everything, but i still get this error. I’m using a XAMPP server on OSX.
You first attempt is almost right, just don’t echo each row, collect the result then echo: