0

I am testing the app I created for possible potential bugs and fixing some security holes into it. It came across my mind to check the database and table of the app exist and if it does it runs the code and if the tables does not exist, it will echo a warning message. However, whenever I am testing a feature, I am getting an error saying, Trying to get property 'project_name' of non-object. Here is the code in my model to which I am testing if the table and column exist:

public function get_project($id){
            if ($this->db->field_exists('id','projects') == FALSE) {

                $this->db->where('id', $id);

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

                    if ($query->num_rows() > 0) {
                        return $query->row();   
                    }return false;
            }else{
                echo "Table and column does not exist!";
            }
        }

As you can see, the line where in I am testing if the field exist where the value is FALSE in order for me to display the error warning in the else statement. However, whenever I am doing this, I am getting an error saying Trying to get property 'project_name' of non-object in my views. Here is the code in my view:

    <h3>Project Name:<?php echo $project_data->project_name; ?></h3>
    <h3>Date Created:<?php echo $project_data->date_created; ?><h3>
    <h3>Description:</h3>
    <p class='projects-description'>
        <?php echo $project_data->project_body; ?>
    </p>
aries
  • 64
  • 1
  • 12

2 Answers2

0

you have the wrong testing for the field if it is exist or not,

please change this line from:

if ($this->db->field_exists('id','projects') == FALSE) {

To

if ($this->db->field_exists('id','projects') != FALSE) {

then you will be good to go!

Edit, Surround the above code inside

if ($this->db->table_exists('yourtablename') )
{
  // table exists
}
else
{
  // table does not exist
}
Ray A
  • 1,283
  • 2
  • 10
  • 22
  • I did try your suggestion but when I tried, it is not giving me the warning message that I am creating in the **else** statement instead displaying a **Database Error Message** – aries Sep 30 '18 at 08:30
  • exactly, now what is the error message, can you post it? – Ray A Sep 30 '18 at 08:38
  • This is the error message displaying **A Database Error Occurred Error Number: 1146 Table 'errand_db.project' doesn't exist SHOW COLUMNS FROM `project` Filename: D:/xampp/htdocs/code_igniter/system/database/DB_driver.php Line Number: 691** instead of the **else** statement I am creating – aries Sep 30 '18 at 08:40
  • @RayA seem to be right. Your table called `projects`? (look at the name in the error). Also, consider the case where the table and column exist but no ID -> then the function return false -> no object... – dWinder Sep 30 '18 at 08:42
  • I see your point, @DavidWinder. But what if the column name or table truly does not exist? I want to throw a message if that column name or table name does not exist instead of displaying the default database error of CodeIgniter if the table does not exist – aries Sep 30 '18 at 08:44
  • check the updates I did in my answer, you should confirm the table existence first, then columns comes next – Ray A Sep 30 '18 at 08:57
  • 1
    i can understand you wanting to do this if the system was dynamic, but to me it doesn't make any sense as `projects` is hardcoded hence the table at the very least should exist as should the id field. this is a lot of extra querying for no particular reason. i would just do `if ($query && $query->num_rows() > 0)` remove the field exists and call it a day. db_debug would be off in prod env and $query would return false if the table or field didn't exist – Alex Sep 30 '18 at 08:57
0

you have wrong if statement it is true or blank, please change this line :

      if ($this->db->field_exists('id','projects')==FALSE) {

use this line

       if ($this->db->field_exists('id','projects')) {

here ..

       public function get_project($id){

        if ($this->db->field_exists('id','projects')) { //its already check true 

            $this->db->where('id', $id);

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

                if ($query->num_rows() > 0) {
                    return $query->row();   
                }return false;
        }else{
            echo "Table and column does not exist!";
        }
    }

}