-1

I'm trying to run this PHP script to get some data from a web service, it works perfectly fine 50~120 times, but then it throws this exception. This is my controller:

 /* $dataArray = Array(
  *                 "Column1" => "value1",
  *                 "Column2" => "value2",
  *                 and so on...);
  */
    foreach ($dataArray as $row => $value)
    {
// $value[$PrimaryKey]  >>> where condition
// $PrimaryKey          >>> name of the column
// $Table               >>> table name
        if($this->m_myModel->verifyRow($value[$PrimaryKey], $PrimaryKey, $Table))
        {
            $newArray[] = $value;
            unset($dataArray[$row]);
        }// if
    }// foreach

And my Model:

function verifyRow($where, $select, $table)
{

    //$this->db->query("SELECT $select FROM $table WHERE $select = $where LIMIT 1");
    $this->db->select($select)
        ->from($table)
        ->where($select, $where)
        ->limit(1);

    $query = $this->db->get();

    if(isset($query->result()[0])) // This is where the error occurs
        return true;
    else
        return false;
}// function verifyRow

I forgot that I changed the $db['default']['db_debug'] in config files to FALSE, so I was not getting any DB error, then I printed $this->db->_error_message(); and got this: MySQL server has gone away

5 Answers5

0

Try like this

$query = $this->db->get();
$result = $query->result(); # added

if(!empty($result))
    return true;
else
{
    return false;
}

And $result can present as follows

foreach ($result as $value) {
    echo $value['field_name'];
}

or

$name = $result[0]['field_name'];
echo $name;
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

i guess that you are trying to get if there is any result Right?

this is a good option also:

if($query->num_rows() > 0) 
        return true;
    else
        return false;
TiGreX
  • 1,602
  • 3
  • 26
  • 41
  • from this you have missed `$query->result()`. Means Processing/Formating Data – Abdulla Nilam Aug 10 '16 at 15:32
  • @Spartan is not necessary get this `result()` see here : https://www.codeigniter.com/userguide3/database/results.html#result-helper-methods – TiGreX Aug 10 '16 at 15:38
  • You are out of knowledge. Take count is diff and processing array is diff – Abdulla Nilam Aug 10 '16 at 15:39
  • I know what you mean, if you want to get information later, you have to do the `result()`, but if you see his code, he is just comparing if exist, which is enough with `num_rows()` – TiGreX Aug 10 '16 at 15:43
  • But when teaching someone you have to give OP full knowledge. Else OP need to post another question for it – Abdulla Nilam Aug 10 '16 at 15:45
  • Yeah, I just want to see if the data exist in the table. And I also used $query->num_rows(), $this->db->count_all_results() before, still get the same error :( – Tarsísio Xavier Aug 10 '16 at 17:08
  • hum, that should work, can you debug and see `$query` value also try this `$count = count($query->results()) ` or `$count = count($query->row_array())` ans see the result of $count – TiGreX Aug 10 '16 at 17:29
  • Got the same error, looks like when I call anything from $this->db->get() it gives the fatal error – Tarsísio Xavier Aug 10 '16 at 18:07
  • can you try to do this query with the sql code i mean `select * from table where...` and see if is working, but instead of do the select do `$query= $this -> db -> query('select statement')` then `var_dum($query->num_rows())` and check also `$query` this is to know also if the query is correct (i suposse yes, but who knows) – TiGreX Aug 10 '16 at 19:36
  • I updated my question. I had thought that would be something wrong with the query, so I printed $this->db->_error_message() and got this: MySQL server has gone away. I'm searching a solution for this now, ty – Tarsísio Xavier Aug 11 '16 at 12:16
0
public function affiche()
     {
       // return $this->db->select('*')->from('produit')->get()->result();
         //   $sql = "SELECT * FROM produit";
        //$result= $this->db->query($sql)->get();
         /*if(!empty($result)){
            return true;   
         }else{
             return false;
         }*/
        $results = array();
        $this->db->select('*');
        $this->db->from('produit');
        $query= $this->db->get();
          if($query == 1) {
               $results = $query->result();
               return  $results;
            }
            else{
                 return false;
            }
     }
  • 1
    Hi there and welcome at stackoverflow. Can you please provide some information about your Code? Just a few lines, why you think that this will solve the problem and what was the problem with the questions code. – bratkartoffel Sep 18 '19 at 14:22
0

The following setup could be used:

public function index()
{
    $result = $this->M_ajout->affiche();
    $data_vue['donne'] = $result;
    $data['vuecharge'] = 'V_produit';
    $data['data_vue'] = $data_vue;
    $this->load->view('template',$data);
}
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
  • 3
    Code only answers are not helpful for future readers. A textual explanation of the code would be helpful – chevybow Sep 18 '19 at 14:25
0
<?php 
                    foreach ($donne as $row){ 
                    ?>

                         <div class="col-sm-3">
                    <center><?=$row->code_type_produit; ?></center><br> 
                        <div class="product-image-wrapper">
                            <div class="single-products">
                                <div class="productinfo text-center">               
                             <a href="<?php /*echo site_url("C_produit/modif?id=$row->")*/ ?>">
                                <img class="imgbox"  src="<?php echo base_url() ?>images/<?php echo $row->img_produit;?>"
                                      width="300px" height="300px" ></a>
                                        <h3><?=$row->code_produit ;?></h3>
                                        <h2><?=$row->prix_produit; echo " CFA";  ?></h2>
                                        <a href="<?php  echo site_url('C_login/authen')?>" class="btn btn-default add-to-cart">
                                                <i class="fa fa-shopping-cart"></i>Voir</a>
                                     <!-- <button class="btn btn-default my-cart-btn" 
                                     data-id="<?=$row->idp ;?>" 
                                     data-name="<?=$row->code_produit ;?>" 
                                     data-price="<?=$row->prix_produit ;?>" 
                                     data-quantity="1"
                                     data-image="<?php echo base_url() ?>images/<?php 
                                     echo $row->img_produit;?>">
                                             Ajouter 
                                    </button> -->
                                    </div>
                                <div class="product-overlay">
                                        <div class="overlay-content">           
                                <img src="<?php echo base_url() ?>images/<?php echo $row->img_produit;?>"
                                          width="150px" height="300px"></a>
                                            <h2><?=$row->Commentaire ;?></h2>
                                            <h2><?=$row->prix_produit; echo " CFA";  ?></h2>
                                            <p><?=$row->code_produit ;?></p>

                                            <a href="<?php  echo site_url('C_login/authen')?>" class="btn btn-default add-to-cart">
                                                <i class="fa fa-shopping-cart"></i>Voir</a>
                                        </div>
                                    </div>
                            </div>

                        </div>

                    </div>
                     <?php } ?>