0

How to return data to a jquery ajax callback function in which the data is coming from a PHP mysql query..

<script language="javascript" type="text/javascript">
   $(document).ready(function() {
        $.post("getMySqlData", function(data){

        });
   });
</script>

here's the PHP code which perform the mysql query to select data from a mysql database

  <?php
    $host = "127.0.0.1";
    $user = "root";
    $password = "problem2";
    $database = "scsi_test_log";
    $cxn = mysqli_connect($host, $user, $password, $database) or die ("couldnt connect to server");

    $query = "SELECT test_header FROM scsi_test";
    $result = mysqli_query($cxn, $query) or die ("Couldn't execute query.");
  ?>
1

1 Answer 1

2

Fetch the result into an array, and echo that in JSON encoding;

$rows = array();
while ( $row = mysqli_fetch_assoc( $result ) ) {
    $rows[] = $row;
}

echo json_encode( $rows );
Sign up to request clarification or add additional context in comments.

1 Comment

Does the echo return this data back to the callback function of the jquery ajax?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.