0

I have a jQuery ajax function which runs a php file to just return all the rows of data from a MySQL table. I can see in dev tools that my PHP file is echoing the data as json but my ajax function always runs the error function and not the success function. Am I missing something really obvious? Any help would be appreciated.

JS

$("#RUN").click(function() {
        $.ajax({
            url: 'api.php',
            data: '',
            dataType: 'json', 
            success: function(data){
                alert('Success');
            },
            error: function(){
                alert('error');
            }
        })
    });

PHP

<?php 
  $databaseName = "workingwithmysql";
  $tableName = "users";
  $connection = mysql_connect("localhost","root",""); 
  $dbs = mysql_select_db($databaseName, $connection);
  $result = mysql_query("SELECT * FROM $tableName");    
  $array = mysql_fetch_row($result);                             
?>
Matrix
  • 21
  • 7
  • 1
    Are you actually returning the data? You just show it being assigned to `$array`, but it never gets sent anywhere. – aynber Feb 23 '16 at 16:31
  • Sorry, I missed that bit off my PHP. I'm doing this at the end: echo json_encode($array); but still the ajax runs the error function. – Matrix Feb 23 '16 at 16:41
  • An Ajax response results in *error* if no data is retrieved. To test, remove `dataType` and just `echo "response";` in the php script and `alert(data);` in the jQuery success function. – Xorifelse Feb 23 '16 at 16:44
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 23 '16 at 16:48
  • Are you only expecting one row of data? – Jay Blanchard Feb 23 '16 at 16:49
  • This is very strange but if I `alert(data);` in my error function and I inspect it I'm seeing the data. – Matrix Feb 23 '16 at 16:52

2 Answers2

2

Is that PHP code snippet complete??

add these two lines:

header('Content-Type: application/json');
echo json_encode($array);

Edit: also.. open the developer tools of your browser... there's probably a "network" tab... use it to view the server's ajax response.

Brad Kent
  • 4,982
  • 3
  • 22
  • 26
  • I'm ending my PHP with the usual echo json_encode($array); but doesn't return the data in the ajax function. I can see the response from the PHP and its as expected but the ajax function just runs the error function and nothing else. @bradkent – Matrix Feb 23 '16 at 16:39
  • @Matrix are you expecting only one row of data? Is your JSON broken? – Jay Blanchard Feb 23 '16 at 16:50
  • No it should return multiple rows of data. If I change the `dataType: 'text'` I then get the data returned. I think my returned data is trying to include the PHP error as well as the actual data (the error is saying that mysql functions are outdated etc.) – Matrix Feb 23 '16 at 16:59
0

Try that :

<?php
    ob_start();
    $databaseName = "workingwithmysql";
    $tableName = "users";
    $connection = mysql_connect("localhost","root",""); 
    $dbs = mysql_select_db($databaseName, $connection);
    $result = mysql_query("SELECT * FROM $tableName");    
    $array = mysql_fetch_row($result);
    ob_end_clean();
    header('Content-Type: application/json');
    echo json_encode($array);
?>

But you really should consider using PDO for your queries to databases.

T'lash
  • 552
  • 1
  • 3
  • 15