Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i am trying to fetch a PHP script using AJAX and return the values as JSON. For some reason, my script fails and i am trying to figure out the problem. When i enter a value form the database into the address bar, like

www.someaddress/post.php?kinaseEntry=aValue

i get a json out put like so;

{"kinaseSKU":null,"url":null,"molecularWeight":null,"tracerSKU":null,"antiSKU1":"antiSKU1","antiSKU2":"antiSKU2","bufferSKU":"bufferSKU","tracerConc":null,"assayConc":null}

My php file looks like so;

<?php

//Include connection to databse require_once 'connect.php';

$kinase = mysql_real_escape_string ($_POST["kinaseEntry"]);

mysql_query('SET CHARACTER SET utf8'); 

$findKinase = "SELECT * FROM kbaData where cleanSKU = '" .$kinase. "' ";

if ($result = mysql_query($findKinase)) {

    $row = mysql_fetch_array($result, MYSQL_ASSOC);

    $kinaseSKU = $row['cleanSKU'];
    $url = $row['url'];
    $molecularWeight = $row['molecularWeight'];
    $tracerSKU = $row['tracerSKU'];
    $antiSKU1 = $row['antiSKU1'];
    $antiSKU2 = $row['antiSKU2'];
    $bufferSKU = $row['bufferSKU'];
    $tracerConc = $row['tracerConc'];
    $assayConc = $row['assayConc'];


    /* JSON ROW */
    $json = array ("kinaseSKU" => $kinaseSKU, "url" => $url, "molecularWeight" => $molecularWeight, "tracerSKU" => $tracerSKU, "antiSKU1" => $antiSKU1, "antiSKU2" => $antiSKU2, "bufferSKU" => $bufferSKU, "tracerConc" => $tracerConc, "assayConc" => $assayConc );

} else  {

    /* CATCH ANY ERRORS */
    $json = array('error' => 'Mysql Query Error');
}

/* SEND AS JSON */
header("Content-Type: application/json", true);

/* RETURN JSON */
echo json_encode($json);

/* STOP SCRIPT */
exit;

?>

Am I going about this the wrong way? or have i done this wrong?

EDIT: Here is my jquery/ajax that calls the php script;

$(document).ready(function() {

$('#kinaseEntry').change(function ()    {

    var kinaseEntry = $('#kinaseEntry').val();
    var dataString = 'kinaseEntry' + kinaseEntry;

    $('#waiting').show(500);
    $('#message').hide(0);
    alert(kinaseEntry);

    //Fetch list from database
    $.ajax({
        type : "POST",
        url : "post.php",
        datatype: "json",
        data:   dataString,
        success : function(datas)   {
            alert("datas" + datas);
        },
        error : function(error) {
            alert("Oops, there was an error!");
        }
    });
    return false;
});

});

share|improve this question
    
Define: "my script fails". –  sberry Feb 9 '12 at 14:34
    
Did you var_dump($row) to be sure it isn't all NULLs? Your code looks correct otherwise. –  Michael Berkowski Feb 9 '12 at 14:34
    
@sberry The failure is in the question title - JSON is all null values... –  Michael Berkowski Feb 9 '12 at 14:35
    
$findKinase returns only a single row, or is it possible, that you get more than one row back ? Looks like you're missing a loop there. –  Norris Feb 9 '12 at 14:38
    
when i use var_dump($row) I get bool(false) –  JPK Feb 9 '12 at 14:44
show 1 more comment

4 Answers

up vote 0 down vote accepted

First I am not sure if if (isset($_POST['kinaseEntry'])) will work for what you have shown. The URL you have shown is a get request, so if you want access to that variable you will have to use $_GET['kinaseEntry']. If you need to do POST change the method attribute in your form to <form method="POST"> that will give you the post variable.

share|improve this answer
    
i remove the isset feature when testing –  JPK Feb 9 '12 at 14:46
    
you were indeed correct. After mashing my code with some of the examples provided here, the last thing i changed was the POST to GET and it now returns the values as it should. Thanks to all who took the time to answer my problem –  JPK Feb 9 '12 at 15:48
add comment

The mysql_query function returns a true value even if 0 rows are returned. It returns false only when there was a database error. So i believe it executes a query and there are no results, so there are null values in the JSON. Try to use mysql_num_rows:

$findKinase = "SELECT * FROM kbaData where cleanSKU = '" .$kinase. "' ";

if ($result = mysql_query($findKinase)) {
      $json = array('error' => 'Mysql Query Error');
}else{
    $num_rows = mysql_num_rows($result);
    if($num_rows > 0){
         // Do sth with the results
    }else{
         $json = array('error' => 'No results');
    }
}
share|improve this answer
    
have tried this fix, unfortunately i get >{"error":"Mysql Query Error"} back –  JPK Feb 9 '12 at 14:55
    
So you have error in the query. You can display the error using mysql_error. –  Secator Feb 9 '12 at 14:56
    
sorry that message was intended for another member, i will try your solution just now –  JPK Feb 9 '12 at 15:05
    
have tried your example, but unfortunately i am getting a blank screen with no results –  JPK Feb 9 '12 at 15:34
add comment

Your problem appears to be that there wasn't actually a row returned from your query. Try:

$result = mysql_query($findKinase);
if ($result !== false && mysql_num_rows($result) > 0) {
    // your code
}

$result is false in this case if there was an error in the query. If there was no error, then check to see if there were actually rows returned. If there were more than 0 rows returned, then attempt to extract data.

share|improve this answer
    
unfortunately, this returns {"error":"Mysql Query Error"} –  JPK Feb 9 '12 at 15:04
    
@JPK I don't think that's unfortunate, I think that means the problem isn't where you thought it was. You need to debug your actual query now. =) –  Crontab Feb 9 '12 at 15:14
add comment

Yeah, you're getting back an array of rows, so that doesn't really work as you'd expect it to. This should fix it, but I don't think it is the best approach (but I think it should work anyway).

$kinase = mysql_real_escape_string ($_POST["kinaseEntry"]);

mysql_query('SET CHARACTER SET utf8'); 

$findKinase = "SELECT * FROM kbaData where cleanSKU = '" .$kinase. "' , LIMIT 0,1";

if ($result = mysql_query($findKinase)) {

    $row = mysql_fetch_array($result, MYSQL_ASSOC);

    $kinaseSKU = $row[0]['cleanSKU'];
    $url = $row[0]['url'];
    $molecularWeight = $row[0]['molecularWeight'];
    $tracerSKU = $row[0]['tracerSKU'];
    $antiSKU1 = $row[0]['antiSKU1'];
    $antiSKU2 = $row[0]['antiSKU2'];
    $bufferSKU = $row[0]['bufferSKU'];
    $tracerConc = $row[0]['tracerConc'];
    $assayConc = $row[0]['assayConc'];


    /* JSON ROW */
    $json = array ("kinaseSKU" => $kinaseSKU, "url" => $url, "molecularWeight" => $molecularWeight, "tracerSKU" => $tracerSKU, "antiSKU1" => $antiSKU1, "antiSKU2" => $antiSKU2, "bufferSKU" => $bufferSKU, "tracerConc" => $tracerConc, "assayConc" => $assayConc );

} else  {

    /* CATCH ANY ERRORS */
    $json = array('error' => 'Mysql Query Error');
}

/* SEND AS JSON */
header("Content-Type: application/json", true);

/* RETURN JSON */
echo json_encode($json);

/* STOP SCRIPT */
exit;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.