0

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.')';
?>
jelhan
  • 6,149
  • 1
  • 19
  • 35
miatech
  • 2,150
  • 8
  • 41
  • 78
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) which has been **removed** entirely from the all supported versions of PHP. You should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php) and upgrade to a version of PHP which receives security fixes. – Quentin Jan 30 '19 at 17:39
  • No his problem is he connects with the "mysql_connect" instead of "mysqli_connect" – Baine Sumpin Feb 03 '19 at 06:34

0 Answers0