Welcome Guest, Not a member yet? Register   Sign In
check if a table exists, if not, create one
#1

[eluser]ajsie[/eluser]
whenever i use

Code:
// check if table exists
    $query = $this->db->query(
        "SELECT *
        FROM contacts
        LIMIT 1"
    );

to see if a table exists CI shows an error page if it doesnt exist.

i want it to not give me an error. if it doesnt exist i want to create the table.

but at the moment the error page is preventing me from doing so.

could i disable the "ERROR: TABLE DOESNT EXIST" error page?
#2

[eluser]eoinmcg[/eluser]
try
Code:
if ($this->db->table_exists('contacts') )
{
  // table exists
}
else
{
  // table does not exist
}
#3

[eluser]mddd[/eluser]
edit
Of course, using the CI method for this is even easier than my suggestion below.


Why not use the Mysql command to do that:
Code:
SHOW TABLES
It will get you a result set containing the names of all existing tables. Just check if your table is in that list.
#4

[eluser]scieck[/eluser]
To check if a table exists, could you use this instead ?
Code:
// check if table exists
$query = $this->db->query("SHOW TABLES LIKE contacts");
#5

[eluser]ajsie[/eluser]
[quote author="scieck" date="1271333455"]To check if a table exists, could you use this instead ?
Code:
// check if table exists
$query = $this->db->query("SHOW TABLES LIKE contacts");
[/quote]

yes it worked. but you had to have single quotes around 'contacts'
#6

[eluser]Prophet[/eluser]
You could just let MySQL do the logic:
Code:
CREATE TABLE IF NOT EXISTS 'table_name'

And generate the page based on the result of this query (TRUE/FALSE).




Theme © iAndrew 2016 - Forum software by © MyBB