Jump to content

[SOLVED] Db::getInstance()->execute($sql) return no value


Recommended Posts

class GetVal{
        public function value(){
            $sql = new DbQuery();
            $sql->from('ps_table');
            $sql->select('id_name');
            $sql->where('id_addr = some street 10');
            return Db::getInstance()->execute($sql); // return ONE name
        }
    }

 

I try to get the result in other class,but get nothing

 

$ddb = new GetVal();
$result = $ddb->value();

 

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

classes: GetValClass.php

if (!defined('_PS_VERSION_')) {
    exit;
}
 
class GetVal
{
    protected $module = false;
    protected $name_of_class;
    protected $context;

    public function __construct(Module $module = null)
    {
        $this->name_of_class = get_class($this);
        $this->module = $module;
        $this->context = Context::getContext();
    }

    public function value($filter)
    {        
        if (!$filter) {return;}		
        $sql = new DbQuery();
        $sql->from('ps_table');
        $sql->select('id_name');
        $sql->where('id_addr = '.$filter);
        return Db::getInstance()->executeS($sql);
    }
}
    

 

your module:

if (!defined('_PS_VERSION_')) {
    exit;
}

require_once(dirname(__FILE__).'/classes/GetValClass.php');
class myModule extends Module
{
    public function __construct()
    {
        ....
    }    
    

    public function getValueInMyClass()
    {
        $ddb = new GetVal($this);
        $filter = addslashes("'some street 10'");
        $result = $ddb->value($filter );
        $idsName = array();
		
        if ($result) {
            foreach ($result as $data){
                $idsName[] = $data['id_name'];
            }

            if ($idsName){
                echo implode(', ',$idsName); 
            }
         }
     }

}

 

Edited by 4you.software (see edit history)
  • Thanks 1
Link to comment
Share on other sites

7 hours ago, 4you.software said:

classes: GetValClass.php

if (!defined('_PS_VERSION_')) {
    exit;
}
 
class GetVal
{
    protected $module = false;
    protected $name_of_class;
    protected $context;

    public function __construct(Module $module = null)
    {
        $this->name_of_class = get_class($this);
        $this->module = $module;
        $this->context = Context::getContext();
    }

    public function value($filter)
    {        
        if (!$filter) {return;}		
        $sql = new DbQuery();
        $sql->from('ps_table');
        $sql->select('id_name');
        $sql->where('id_addr = '.$filter);
        return Db::getInstance()->executeS($sql);
    }
}
    

 

your module:

if (!defined('_PS_VERSION_')) {
    exit;
}

require_once(dirname(__FILE__).'/classes/GetValClass.php');
class myModule extends Module
{
    public function __construct()
    {
        ....
    }    
    

    public function getValueInMyClass()
    {
        $ddb = new GetVal($this);
        $filter = addslashes("'some street 10'");
        $result = $ddb->value($filter );
        $idsName = array();
		
        if ($result) {
            foreach ($result as $data){
                $idsName[] = $data['id_name'];
            }

            if ($idsName){
                echo implode(', ',$idsName); 
            }
         }
     }

}

 

Thanks,The issue is solved

Link to comment
Share on other sites

  • sphway changed the title to [SOLVED] Db::getInstance()->execute($sql) return no value

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