0
mysql_connect('localhost:3036', 'x', 'x');
mysql_select_db('extractor');
$baseSKUraw = mysql_query("SELECT * FROM product_category where tier_one='".$result1."' and tier_two ='".$result2."' ");
$baseSKU = mysql_fetch_array($baseSKUraw);
echo json_encode(array("error"=>0, "result1"=>$baseSKU['sku_base']));

The Json is returning {"error":0,"result1":null} but when I do a "result1"=>"texthere" it will return accordingly to my textbox.

  • What went wrong here, I can't seem to display the sku_base?
  • When should I use mysql_fetch_array? because I'm returning only 1 result now?

var_dump(baseSKUraw);

resource(3) of type (mysql result)
{"error":0,"result1":null}

print_r($baseSKU);

resource(3) of type (mysql result)
Array
(
    [0] => 1
    [id] => 1
    [1] => Tops
    [tier_one] => Tops
    [2] => Shortsleeve
    [tier_two] => Shortsleeve
    [3] => WTSS
    [sku_base] => WTSS
)
7
  • Should this be marked as javascript not java? Commented Jun 20, 2013 at 4:05
  • Did you try to do a var_dump($baseSKUraw), to see you get any results? Anyway try to use MYSQLi or PDO_MYSQL instead. all mysql_ functions are deprecated Commented Jun 20, 2013 at 4:08
  • add echo mysql_error(); before your json_encode line, and then update the question with the output. Commented Jun 20, 2013 at 4:15
  • Check the result of print_r($baseSKU); Commented Jun 20, 2013 at 4:16
  • 1
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Jun 20, 2013 at 4:39

1 Answer 1

1

Your problem is here,

$baseSKU = mysql_fetch_array($baseSKUraw);
echo json_encode(array("error"=>0, "result1"=>$baseSKU['sku_base']));

your using mysql_fetch_array and trying to use the results as if it was returned using mysql_fetch_assoc. When you use mysql_fetch_array you need to use the numerical index.

Sign up to request clarification or add additional context in comments.

5 Comments

The default return mode of mysql_fetch_array() returns an array with both associative and numeric keys...
Hi @DevZer0 , Thank you. Can you explain the diff between array and assoc? Which is the correct one i should be using. how do you debug ajax? because print_r var_dump wont show unless i return json_encode. At the same time, i see people using print_r , var_dump , what should i use, both? btw i use firebug to check of json response, i think it's working good
just use the one that you feel gives you the best output, i personally use print_r
associative array is once you can access with a key index like $foo['hello'], $bar['world']. The other type is numeric and they have a 0 based numeric index.
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

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.