0

I want to check if a table in the OpenCart database exist so i made this function

public function CheckCustomer(){
$query = $this->db->query('SELECT * FROM '.DB_PREFIX.'customer_online');
return $query->row;
}

and in my controller i test if it exists i set a variable to 1 or 0 depending.

The table does exist everything is fine, byt if i delete the c from customer just to simulate the table not being there my tpl page is not rendered instead i get this error :

Notice: Error: Table 'OpenCart-Test.oc_ustomer_online' doesn't exist Error No: 1146 SELECT * FROM oc_ustomer_online in /home/justine/www/opencart-test/opencart-1.5.5.1/upload/system/database/mysql.php on line 50

Is there anyway of doing this without it throwing errors on screen as i need to know if the table exists before i display certain information in my tpl file.

Hope someone can shed some light on this.

  • Please, look on similar topics [here](http://stackoverflow.com/questions/6432178/how-can-i-check-if-a-mysql-table-exists-with-php) or [here](http://stackoverflow.com/questions/9008299/check-if-mysql-table-exists-or-not) – Mikhail Chernykh Mar 21 '13 at 19:31

3 Answers3

0

Generally speaking dynamically-created tables are a bad idea. Any table you need should always exist, and you can simply use TRUNCATE TABLE mytable to quickly remove all rows rather than DROP TABLE.

edit: way better idea than below
Change the query to SHOW TABLES FROM my_database [or simply SHOW TABLES if the db is already selected] and check to see if the table name you're looking for exists in the result set.


That said, you should be able to suppress the error by prefixing the function with @, ie:

$query = @$this->db->query('SELECT * FROM '.DB_PREFIX.'customer_online');

Or you should be able to switch your mySQL database object to use Exceptions rather than PHP Errors, then enclose the function call in a try{ } catch() { } block.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • Didn't work sorry. The reason i need to test is that earlier versions of Opencart pre 1.54 dont have the table customer_online . – Justine Smithies Mar 21 '13 at 19:35
  • @JustineSmithies you'd probably be better served using the [`VERSION`](https://github.com/opencart/opencart/blob/master/upload/index.php) var declared in OpenCart rather than a call to a deprecated function. – Sammitch Mar 21 '13 at 21:13
0

Resolved .

Putting this in my controller file fixed it.

$this->data['checkcustomer'] = @mysql_query ('SELECT * FROM '.DB_PREFIX.'customer_online');
0

MySQL can list all tables in a database. By specifying a "like tablename" you can search for a specific table.

/**
 * Check if the table 'customer_online' exists
 * @return boolean TRUE if table exists, FALSE otherwise.
 */
public function CheckCustomer(){
    $res = $this->db->query("SHOW TABLES LIKE '".DB_PREFIX."customer_online'");
    return (boolean) $res->num_rows;
}

This does not generate any errors as it is not trying to access the table, just looking for the table name in the list of tables in the database.

Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129