CodeIgniter Forums

Full Version: How to fix a Num_row error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey!!

Im trying to fix an error assciated with this code (  if($result->num_rows > 0);{

                while($row = $result->fetch_assoc()){
                    $data[] = $row;

                }
            }
        }
        return !empty($data)?$data:false;
    }
   

):


The error is:
A PHP Error was encountered

Severity: Notice
Message: Trying to get property of non-object
Filename: views/DB.class.php
Line Number: 72



How do I fix this, Ive been wrecking my brain trying to figure this out Sad 



Heart Heart ,
Mekaboo
@Mekaboo,

I see a couple of things.

1. this... if($result->num_rows > 0) should be ($result->num_rows() > 0)
2. Your brackets don't line up. Is it possible to see all of the code (formatted)
also remove semicolon:

if($result->num_rows() > 0);{
(06-24-2019, 09:59 PM)php_rocs Wrote: [ -> ]@Mekaboo,

I see a couple of things.

1. this... if($result->num_rows > 0) should be ($result->num_rows() > 0)
2. Your brackets don't line up.  Is it possible to see all of the code (formatted)

Sure thing here is all the code:

<?php
/*
 * DB Class
 * This class is used for database related (connect, insert, update, and delete) operations
 * @author    CodexWorld.com
 * @url        http://www.codexworld.com
 * @license    http://www.codexworld.com/license
 */
class DB{
    private $dbHost     = "50.87.144.41";
    private $dbUsername = "cultured_root";
    private $dbPassword = "brownie81";
    private $dbName     = "cultured_codeignite";
    
    public function __construct(){
        if(!isset($this->db)){
            // Connect to the database
            $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
            if($conn->connect_error){
                die("Failed to connect with MySQL: " . $conn->connect_error);
            }else{
                $this->db = $conn;
            }
        }
    }
    
    /*
     * Returns rows from the database based on the conditions
     * @param string name of the table
     * @param array select, where, order_by, limit and return_type conditions
     */
    public function getRows($table, $conditions = array()){
        $sql = 'SELECT ';
        $sql .= array_key_exists("select",$conditions)?$conditions['select']:'*';
        $sql .= ' FROM '.$table;
        if(array_key_exists("where",$conditions)){
            $sql .= ' WHERE ';
            $i = 0;
            foreach($conditions['where'] as $key => $value){
                $pre = ($i > 0)?' AND ':'';
                $sql .= $pre.$key." = '".$value."'";
                $i++;
            }
        }
        
        if(array_key_exists("order_by",$conditions)){
            $sql .= ' ORDER BY '.$conditions['order_by'];
        }else{
            $sql .= ' ORDER BY id DESC ';
        }
        
        if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
            $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit'];
        }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
            $sql .= ' LIMIT '.$conditions['limit'];
        }
        
        $result = $this->db->query($sql);
        
        if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
            switch($conditions['return_type']){
                case 'count':
                    $data = $result->num_rows;
                    break;
                case 'single':
                    $data = $result->fetch_assoc();
                    break;
                default:
                    $data = '';
            }
        }else{
           if($result->num_rows() > 0); {
                while($row = $result->fetch_assoc()){
                    $data[] = $row;

                }
            }
        }
        return !empty($data)?$data:false;
    }
    
    /*
     * Insert data into the database
     * @param string name of the table
     * @param array the data for inserting into the table
     */
    public function insert($table, $data){
        if(!empty($data) && is_array($data)){
            $columns = '';
            $values  = '';
            $i = 0;
            if(!array_key_exists('created',$data)){
                $data['created'] = date("Y-m-d H:iConfused");
            }
            if(!array_key_exists('modified',$data)){
                $data['modified'] = date("Y-m-d H:iConfused");
            }
            foreach($data as $key=>$val){
                $pre = ($i > 0)?', ':'';
                $columns .= $pre.$key;
                $values  .= $pre."'".$this->db->real_escape_string($val)."'";
                $i++;
            }
            $query = "INSERT INTO ".$table." (".$columns.") VALUES (".$values.")";
            $insert = $this->db->query($query);
            return $insert?$this->db->insert_id:false;
        }else{
            return false;
        }
    }
    
    /*
     * Update data into the database
     * @param string name of the table
     * @param array the data for updating into the table
     * @param array where condition on updating data
     */
    public function update($table, $data, $conditions){
        if(!empty($data) && is_array($data)){
            $colvalSet = '';
            $whereSql = '';
            $i = 0;
            if(!array_key_exists('modified',$data)){
                $data['modified'] = date("Y-m-d H:iConfused");
            }
            foreach($data as $key=>$val){
                $pre = ($i > 0)?', ':'';
                $colvalSet .= $pre.$key."='".$this->db->real_escape_string($val)."'";
                $i++;
            }
            if(!empty($conditions)&& is_array($conditions)){
                $whereSql .= ' WHERE ';
                $i = 0;
                foreach($conditions as $key => $value){
                    $pre = ($i > 0)?' AND ':'';
                    $whereSql .= $pre.$key." = '".$value."'";
                    $i++;
                }
            }
            $query = "UPDATE ".$table." SET ".$colvalSet.$whereSql;
            $update = $this->db->query($query);
            return $update?$this->db->affected_rows:false;
        }else{
            return false;
        }
    }
    
    /*
     * Delete data from the database
     * @param string name of the table
     * @param array where condition on deleting data
     */
    public function delete($table, $conditions){
        $whereSql = '';
        if(!empty($conditions)&& is_array($conditions)){
            $whereSql .= ' WHERE ';
            $i = 0;
            foreach($conditions as $key => $value){
                $pre = ($i > 0)?' AND ':'';
                $whereSql .= $pre.$key." = '".$value."'";
                $i++;
            }
        }
        $query = "DELETE FROM ".$table.$whereSql;
        $delete = $this->db->query($query);
        return $delete?true:false;
    }
}
(06-24-2019, 11:30 PM)neuron Wrote: [ -> ]also remove semicolon:

if($result->num_rows() > 0);{

Made the changes and still didnt work, still working on figuring it out.
@Mekaboo,

What error are you seeing?
(06-25-2019, 03:49 PM)php_rocs Wrote: [ -> ]@Mekaboo,

What error are you seeing?

Here is new error:

An uncaught Exception was encountered

Type: Error
Message: Call to a member function num_rows() on boolean
Filename: /home4/cultured/public_html/application/views/DB.class.php
Line Number: 72

For this code:
if($result->num_rows() > 0); {

                while($row = $result->fetch_assoc()){
                    $data[] = $row;
@Mekaboo,

Did you remove the semicolon after the if condition? It should not be there.
(06-25-2019, 10:01 PM)php_rocs Wrote: [ -> ]@Mekaboo,

Did you remove the semicolon after the if condition?  It should not be there.

I did before and still got error. Will try it out again Smile
Why do you use a "third party" database class instead of the built-in database library CI is providing? See https://www.codeigniter.com/userguide3/d...index.html
Just set up the connection paramaters in application/config/database.php and you're good to go.
Start using the query builder right away. It will save you a lot of time and trouble.

Simple example (meant as a function in a model):
PHP Code:
Public function get_products($category=NULL)
{
 
  $this->db
   
->select('id,product_name,category,price')
 
  ->from('products');
 
  If ($category) {
 
    $this->db->where('category'$category);
 
 }
 
 $this->db->order_by('product_name','ASC' );
 
 $query $this->db->get();
 
 If ($query->num_rows() > 0) {
 
    Return $query->result();
 
 }
 
 Else {
 
   Return FALSE;
 
 }

This function will return an array of records (if there are any), or FALSE if there are no records found.
Pages: 1 2