Jump to content

Db::getInstance()->getValue() won't return any value !


Recommended Posts

After I followed strictly the PrestaShop documentation for creating a module I ended up with the module working absolutely fine as it has regarding the official tutorial.

 

Now, I wanted to further access a value in the database and print it out with Simply on my Front End viewer, that I already created during the tutorial.

 

I have tried to achieve this as indicated in the PrestShop documentation but it is not working for me! Like this:

$sql = 'SELECT price FROM '._DB_PREFIX_.'product WHERE id_product = 11';
$customVal = Db::getInstance()->getValue($sql);

I have just added "WHERE id_product = 11" to the example query, the rest is as it shows here on the PS documentation doc.prestashop.com/display/PS15/DB+class+best+practices (at the end of the page where it references the getValue() method).

 

I add this two lines of code to my "public function displayForm()" in the module.php file. The structure of this module.php file is exactly as it is shown in the tutorial here http://doc.prestashop.com/display/PS16/Adding+a+configuration+page

 

After this is done, in my module.tpl file I just do this:

<!-- Block mymodule -->
<div id="mymodule" class="block">
  <h4>Welcome!</h4>
  <div class="block_content">
    <p>
    
    {$customVal}

    </p>   
    <ul>
      <li><a href="{$my_module_link}" title="Click this link">Click me!</a></li>
    </ul>
  </div>
</div>
<!-- /Block mymodule -->

When I load my shop and my module is loaded I see the h4 title "Wellcome!" and the "Click me!" link correctly, but the value I'm trying to extract from the database just shows nothing. It is always empty.

 

There are no errors in the browser console.

My PrestaShop is 1.6.0.9

I tested with Chrome and Firefox, and I am working on localhost with WAMP64

 

 

I also have tried this:

$customVal = Db::getInstance()->executeS('SELECT `custom_field` FROM `'._DB_PREFIX_.'product_lang` WHERE `id_product` = `11` AND `id_lang` = `6`');

And this:

$customVal = Db::getInstance()->getValue("SELECT `price` FROM `"._DB_PREFIX_."product` WHERE `id_product = 11`");

And even this:

$sql = new DbQuery();
$sql->from('product_lang');
$sql->select('custom_field');
$sql->where('id_product = 11');
$sql->where('id_lang = 6');
$customVal = Db::getInstance()->executeS($sql);

Also I have tried many other ways, checked quotes ("" , '' , ``) and etc. Nothing helped.

 

Please, can anyone shed some light on what is going on and why I am not seeing any value.

 

Thanks in advance.

Edited by Logixor (see edit history)
  • Like 1
Link to comment
Share on other sites

I solved it.

 

For anyone who may face the same problem, here is the solution.

 

First of all, I was doing the things in the wrong way, completely wrong way ;D. The problem wasn't the getValue(); method!

It was that I wasn't assigning the extracted value from the database to a "Smarty" value.

 

The way I achieved this is by the following method:

$this->context->smarty->assign('customVal', $customVal);

And before I execute this line, I ofcourse extract the value from the database and store it in the $customVal variable like this:

$sql = new DbQuery();
$sql->from('product');
$sql->select('price');
$sql->where('id_product = 11');
$customVal = Db::getInstance()->getValue($sql);
Link to comment
Share on other sites

  • 2 years later...
  • 2 years later...

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