I'm working on rest api
with php
, mysql
for a quote application. the json
data should be displayed on a webpage (bootstrap) and I have a function javascript
file that runs when the page is ready. the function calls a getjosn
function through jquery
that sends the request to api.php
. when the page opens I'm getting
"internal server error"
when $.getjson
function is executed.
Here's the code:
$(function() {
//variables
var functionName = 'GetProducts';
//Get products
function LoadData() {
$.getJSON("http://localhost/quoteapi/api.php?function="+functionName+"&jsonCallback=?", function(data) {
console.log(data);
});
}
LoadData();
});
api.php
<?php
$connect = mysql_connect('localhost', 'root', 'somepassword', 'quotesapi_db') or die('Database connection error.'. mysqli_error($connect));
if(!isset($_GET['function'])) {
die('some error occurred');
}
function GetProducts($db) {
$sql = mysqli_query($db, 'SELECT * FROM quotes ORDER BY id');
if(mysqli_num_rows($sql) > 0) {
while($row = mysqli_fetch_array($sql)) {
$data[] = $row['quote'];
}
}
print_r($data);
}
//GetProducts($db);
$data = json_encode($data);
echo $_GET['josnCallback'].'('.$data.')';
?>