user3240828
user3240828

Reputation: 9

ajax call to php to get database rows

The following is in my file.js

function mainget(){
    $.ajax({    
          type: 'GET',
          url: 'example.php',
          data:json,
          success:function(data){
          }
    });       
} 

example.php

<?php 
     $con = mysqli_connect('address','DATBASE','pass','futureday');
     $result = mysql_query("SELECT * FROM $futureday");          
     $array = mysql_fetch_row($result);                             
     echo json_encode($array); 
?>

I have been struck with this for the past 2 days. I have tried inserting alert as first line of function mainget , which is successful, but after that I get nothing.

Upvotes: 0

Views: 435

Answers (2)

Sherin Jose
Sherin Jose

Reputation: 2516

You are using data property in AJAX call to indicate the json data type. It is an invalid one. Use dataType to provide the data type. data property is used to pass the datas. And also put quotes to the values like:

dataType:'json'

Also change your example.php file. There you are using mysqli_connect to connect the database, then mysql_* to execute and fetch operations. It is not correct. Use either mysqli_* or mysql_*. Edit as:

<?php 
     $con = mysqli_connect('address','DATBASE','pass','futureday');
     $result = mysqli_query("SELECT * FROM $futureday");          
     $response  = array();
     while($array = mysqli_fetch_row($result)){
         $response[]=$array;
     }
     echo json_encode($response); 
?>

Upvotes: 1

ghost
ghost

Reputation: 477

Use this

$mysqli = new mysqli('address','DATBASE','pass','futureday');
           $query = "SELECT * FROM $futureday";  
           $results=$mysqli->query($query) ;        
           $res=$mysqli->fetch_array(MYSQLI_ASSOC);
            echo json_encode($res);               

Upvotes: 0

Related Questions