Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7568375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:54:31+00:00 2026-05-30T14:54:31+00:00

I’m starting to write a simple Model class using codeigniter 2.1.0. All that I

  • 0

I’m starting to write a simple Model class using codeigniter 2.1.0. All that I want it to do for now is create and delete the MySQL table it represents. Here’s the code:

<?php

    class Users_model extends CI_Model {

        public function createTable(){

            if( $this->db->table_exists('users') == FALSE ){

                $query = "CREATE TABLE users(
                        id SERIAL PRIMARY KEY,
                        email  VARCHAR(52) NOT NULL check(email <> ''),
                        UNIQUE (email)
                        )ENGINE = InnoDB;";

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

            }

        }

        public function deleteTable(){

            if( $this->db->table_exists('users') ){

                $query = "DROP TABLE users;";

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

            } else {

                echo "users not found <br />";

                $tables = $this->db->list_tables();
                foreach ($tables as $table)
                {
                    echo $table."<br />";
                }
            }

        }


    }

?>

And this is the code I’m executing in the main class:

$this->load->model('users_model');
$this->users_model->deleteTable();
$this->users_model->createTable(); 

The first run is fine and dandy. The tables are created just fine. But then, when I run it again, I get the output:

users not found
users

Upon further inspection I realize table_exists() never returns TRUE. var_dump($this->db->table_exists('users')); returns bool(false).

So, am I doing something wrong? This is as simple as it gets, copy pasted from the documentation. Google doesn’t return anything related, and being a C programmer my mindset is always “If there’s a bug, it’s yours. (Richard Stallman is never wrong)”… But since Mr. Stallman has little to do with this, there’s a possibility.

TL;DR

Why does table_exists('users') in the above code never returns TRUE even if list_tables() does return ‘users’;

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:54:32+00:00Added an answer on May 30, 2026 at 2:54 pm

    Try using var_dump() instead of echo. Boolean values don’t work well with echo.

    var_dump( $this->db->table_exists('users') );
    

    The source code of table_exists looks like this:

    function table_exists($table_name)
    {
        return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
    }
    

    Update from comments

    If you have a look at _protect_identifiers it does add the database name to the table name. But there should be a . between db.table. Maybe your db config is messed up?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.