php Codeigniter - 在布尔值上调用成员函数 result()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38877635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:44:56  来源:igfitidea点击:

Codeigniter - Call to a member function result() on boolean in

phpmysqlcodeigniterisset

提问by Tarsísio Xavier

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:

我正在尝试运行此 PHP 脚本以从 Web 服务获取一些数据,它可以正常运行 50 到 120 次,但随后会引发此异常。这是我的控制器:

 /* $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

我忘了我将配置文件中的 $db['default']['db_debug'] 更改为 FALSE,所以我没有收到任何数据库错误,然后我打印了 $this->db->_error_message(); 得到了这个:MySQL 服务器已经消失

回答by TiGreX

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;

回答by Liban Gamal

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;
            }
     }

回答by Liban Gamal

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);
}

回答by Liban Gamal

<?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>Vtheitroad</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>Vtheitroad</a>
                                        </div>
                                    </div>
                            </div>

                        </div>

                    </div>
                     <?php } ?>     

回答by Abdulla Nilam

Try like this

像这样尝试

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

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

And $resultcan present as follows

并且$result可以呈现如下

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

or

或者

$name = $result[0]['field_name'];
echo $name;