Reputation: 10042
I have a simple database query written in PHP using PDO. When I var_dump
my $results
, I get an associative array. So I figured I'd just use return $ result
, call the script using AJAX and then work from there. But now when I console.log
the data I get, I just get an empty string.
Can somebody explain what I'm doing wrong and how to fix it? Thanks
Here's my php (I've emptied host, username and password for "safety"):
<?php
try {
$hostname = "";
$username = "";
$password = "";
$db = new PDO("mysql:host=$hostname;dbname=topdecka_PTC",$username, $password);
$raw_result = $db->query('SELECT * FROM articles');
$result = $raw_result->fetchAll(PDO::FETCH_ASSOC);
return $result;
} catch (PDOException $e) {
echo "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
and my AJAX function:
$(document).ready(function() {
$.get( "db_queries/all_articles.php", function( data ) {
console.log( data );
});
});
Upvotes: 0
Views: 94
Reputation: 12236
You need to echo JSON encoded data:
Change this line:
return $result;
To:
echo json_encode($result);
Upvotes: 1