Skip to main content

JSON PHP data returning null



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;

});







});


Comments

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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');
    }
    }

    ReplyDelete
  4. 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;

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex