Jump to content

get the value from database by using DB::getInstance()->Execute() instead of mysqli_query()


Recommended Posts

Hi,

In prestashop I am doing a small module. In that I have some jQuery ajax. For database connection I am using mysqli_connect as mysql_connect has been deprecated.

So my entire script to fetch some data from database through ajax looks like this

 



<?php
include '../../../config/settings.inc.php';
include '../../../config/defines.inc.php';
include '../../../config/config.inc.php';


$connection = mysqli_connect(_DB_SERVER_,_DB_USER_,_DB_PASSWD_) or die(mysqli_error());
mysqli_select_db($connection,_DB_NAME_) or die(mysql_error());
$query = "SELECT DISTINCT `state_name` FROM "._DB_PREFIX_."storeinfo WHERE `country_name`='".$_GET['sta']."'";
$result = mysqli_query($connection,$query) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) { 
  $data=$row['state_name'];
echo '<option value="'.$data.'">'.$data.'</option>';
  }
?>


 

Here its working absolutely fine. But as Prestashop tells to use 

 


DB::getInstance()->Execute() to ge the values instead of mysqli_query

, here I had tried DB::getInstance()->Execute() but it is not working at all. So can someone kindly suggest me how to solve this problem? Any help and suggestions will be really appreciable.

Thanks

 

Link to comment
Share on other sites

 

Hi,
In prestashop I am doing a small module. In that I have some jQuery ajax. For database connection I am using mysqli_connect as mysql_connect has been deprecated.
So my entire script to fetch some data from database through ajax looks like this
 
<?php
include '../../../config/settings.inc.php';
include '../../../config/defines.inc.php';
include '../../../config/config.inc.php';


$connection = mysqli_connect(_DB_SERVER_,_DB_USER_,_DB_PASSWD_) or die(mysqli_error());
mysqli_select_db($connection,_DB_NAME_) or die(mysql_error());
$query = "SELECT DISTINCT `state_name` FROM "._DB_PREFIX_."storeinfo WHERE `country_name`='".$_GET['sta']."'";
$result = mysqli_query($connection,$query) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) { 
  $data=$row['state_name'];
echo '<option value="'.$data.'">'.$data.'</option>';
  }
?>
 
Here its working absolutely fine. But as Prestashop tells to use 
 
DB::getInstance()->Execute() to ge the values instead of mysqli_query
, here I had tried DB::getInstance()->Execute() but it is not working at all. So can someone kindly suggest me how to solve this problem? Any help and suggestions will be really appreciable.
Thanks

 

 

 

Here is the total code by using Db::getInstance() method
 
<?php
include '../../../config/settings.inc.php';
include '../../../config/defines.inc.php';
include '../../../config/config.inc.php';




$connection = mysqli_connect(_DB_SERVER_,_DB_USER_,_DB_PASSWD_) or die(mysqli_error());
mysqli_select_db($connection,_DB_NAME_) or die(mysql_error());
$query = "SELECT DISTINCT `state_name` FROM "._DB_PREFIX_."storeinfo WHERE `country_name`='".$_GET['sta']."'";
$result = Db::getInstance()->getRow($query);//I have tried this one first then I tried the second one below
$result = Db::getInstance()->Execute($query);//also I had tried this one
while($row = mysqli_fetch_array($result)) { 
  $data=$row['state_name'];
echo '<option value="'.$data.'">'.$data.'</option>';
  }
?>
Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

Hello use like this for fetching records from database

my prestashop version is 1.6x

 

you need to use execute() instead of ExecuteS()  B)

 

sql = 'SELECT sum(product_count) AS total_likes_count product_count FROM '._DB_PREFIX_.'count_likes WHERE product_id="'.$product_id.'"';
$results = Db::getInstance()->execute($sql);
$count_total_likes = $results['total_likes_count'];
Link to comment
Share on other sites

  • 3 months later...

Hi, I also have a problem with execute() function. I have a query that looks like this:

$sql_addr = "SELECT a.id_address FROM pspf_address a JOIN pspf_customer b ON a.id_customer=b.id_customer WHERE b.email=".$_POST['customer_id'];
if($results = Db::getInstance(_PS_USE_SQL_SLAVE_)->Execute($sql_addr)){
	$addrID = $results;
} else {
	$addrID = "0";

That SELECT query should return a result with 1 column and 1 row with value of 5 (I tried the query in phpmyadmin).

 

But when it is executed in my php file, it returns value of 1. What did I do wrong?

 

Thank you.

Edited by luthfifr (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...