Tomyi
Tomyi

Reputation: 1

AJAX call returning json array undefined

I've seen that there has been a lot of questions about this but I did not find any specifics that could apply to my case so if I'm creating a duplicate, sorry for that.

I am trying to retrieve data from SQL database with php file that passes the data to ajax call. Everything seems to be working fine, just when I try to output data into console I get "undefined" variable, even when I tried accessing a precise part of the array with data.story for example. I have also tried data[0].story but that gave me an error that 'story' field of undefined cannot be accessed. The code is below:

Thanks for your help guys.

my php file:

<?php
$con = mysqli_connect('localhost','root','password','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$array = array();
$sqlFetch = "SELECT s.storyTitle, s.story, s.lattitude, s.longitude,
u.nickname, u.platformUsed, u.sexuality, u.gender, u.age, s.category,
s.dateRecorded FROM stories AS s INNER JOIN users AS u ON u.email = s.email
WHERE s.postStatus != 'published'";
$result = mysqli_query($con,$sqlFetch);
if(!is_null($result->num_rows)){
$encode = array();
while($row = mysqli_fetch_assoc($result)) {
    $encode[] = $row;
}
echo json_encode($encode);  
}
?>

and ajax code:

$.ajax({
        type: 'post',
        url: "http://localhost/wordpress/wp-content/themes/TinDA/js/loadData.php",
        dataType: 'json',
        data: "",


}).done(function(data){
    console.log(data);
//tried also: console.log(data.story); and data[0].story;


});

Upvotes: 0

Views: 352

Answers (1)

shelton
shelton

Reputation: 11

It seems that you are mixing the mysqli connection for Procedural Style with Object Oriented Style

Procedural:

$con = mysqli_connect('localhost','root','password','db');
$result = mysqli_query($con, "SOME SELECT STATEMENT");

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

$rows = mysqli_num_rows($result);
if($rows){
   json_encode(array('data' => $data, 'msg'=> 'successs'));
} else {
   json_encode(array('data' => $data, 'msg'=> 'error or no records...'));
}

OOP:

$con = new mysqli('localhost','root','password','db');
if($con->connect_errno){
    echo "WTF didn't work!!" . $con->connect_error;
}

$res = $con->query('SOME SELECT STMNT');
while ($row = $res->fetch_assoc()){
    $data[] = $row;
}

$rows = $con->num_rows;
if($rows){
    json_encode(array('data' => $data, 'msg'=> 'successs'));
}else {
    json_encode(array('data' => $data, 'msg'=> 'error or no records...'));
}

I also like to use this version of ajax (different with 3.0 may not work). You can then see the data errors. Note, you can have a successful ajax call and return but still have an error.

$.ajax( '/http://localhost/...', {
    type: 'POST',
    dataType: 'json',
    success: function( json ) {
        console.log( json );
    },
    error: function( req, status, err ) {
        console.log( 'WTF!!', status, err );
    }
});

Upvotes: 1

Related Questions