Search code examples
phparraysprestashop

prestashop module showing Warning: mysql_fetch_assoc() expects parameter 1 to be resource, array given


I am doing a custom module in prestashop. Now in my php file I have the function like

 public function hookHome($params)
  {
    $defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
    global $cookie, $smarty;
    $value=array();
    $sql_select="SELECT DISTINCT country_name,country_ISO from "._DB_PREFIX_."storedetails where status='1'";
       $result=Db::getInstance()->ExecuteS($sql_select);
       print_r($result);

      while($row=mysql_fetch_assoc($result))
      {
      $value[] = $row;
  }
       $smarty->assign('array',$value);
       $smarty->assign('default',$defaultLanguage);
     return $this->display(__FILE__, 'storedetails.tpl');
  }

The concept behind this piece of code is storing all the values in an array, and get them in view file(smarty template).

Here when I am doing print_r($result); it is showing the array. The values are coming like this

Array ( [0] => Array ( [country_name] => [country_ISO] => select ) [1] => Array ( [country_name] => Germany [country_ISO] => DE )...

but from next line it is showing error like Warning: mysql_fetch_assoc() expects parameter 1 to be resource, array given in filename.php line number

In my .tpl file(view file) I have the code where I would like to get the values like

<select onchange="selectCountry(this.value)">
    <option value="Select">Select</option> 
     {foreach from=$array item=row}
         <option value="{$row.country_ISO}" id="{$row.country_name}">{$row.country_name}</option>
    {/foreach}
    </select>

So can someone kindly tell me why I am getting this error and how to solve this issue?Any help and suggestions will be really appreciable. Thanks


Solution

  • Here directly assign your $result to smarty template. Because it is already in array format.

    $smarty->assign('array',$result);
    

    mysql_fetch_assoc is used to iterate the result of SELECT query, but in your situation your are giving array as input not the resource of SELECT query.