Jump to content

Passing database query rows with ajax json


joseluistim

Recommended Posts

Hello.

 

I want to get sql query rows result info through ajax and json.

 

Here´s jquery code:

 

var mod_url = baseDir + '/json_2.php';

 

var query = $.ajax({
  type: 'POST',
  url: mod_url,
  dataType: 'json',
  success: function(comp_arr) {
    alert(comp_arr);
  },
  error: function (response) {
                        alert(response.responseText);
  },
  });
 

And here´s php file code :

 
<?php
include(dirname(__FILE__).'/../config/config.inc.php');
include(dirname(__FILE__).'/../init.php');
$qr = 'SELECT * FROM '._DB_PREFIX_.'infos_it WHERE infoit=1';
if ($results = Db::getInstance()->ExecuteS($qr))
foreach ($results as $row) {
$comp_arr[$i]["id_item"] = $row["id_id_item"];
$comp_arr[$i]["infoit"] = $row["infoit"];
$comp_arr[$i]["icon"] = $row["icon"];
}
 
echo(json_encode($comp_arr));
//return(json_encode($comp_arr));
?>
 

Jquery Ajax Response is success but null...what I´m doing wrong?

 

Thanks.

 

Link to comment
Share on other sites

More information and tests..

 

If I execute same query and var_dump directly to database, with $cfg_server= "localhost"; $cfg_database = "xxxx"; $cfg_username = "xxxxx"; $cfg_password = "xxxxx"; instead of  Db::getInstance()->ExecuteS($qr), it works correctly and return 19 records with all its fields.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...