Answers.Body: <p>If you're writing PHP in a CiviCRM extension - you can add a /sql/install.sql file in which you can execute MySQL commands like:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-15T23:51:29.673Z
Answers.LastActivityDate: 2017-02-15T23:51:29.673Z
Answers.UserDisplayName: KarinG - Semper IT
Answers.Body: <p><a href="https://github.com/civicrm/civicrm-core/blob/master/CRM/Queue/BAO/QueueItem.php#L50" rel="nofollow noreferrer">CRM_Queue_BAO_QueueItem:: findCreateTable()</a> contains an example of how CiviCRM does this, and the presence of this suggested that no "canned" method might exist already.</p>
  
  <pre><code>$checkTableSql = "show tables like 'civicrm_queue_item'";
  $foundName = CRM_Core_DAO::singleValueQuery($checkTableSql);
  if ($foundName == 'civicrm_queue_item') {
    return TRUE;
  }
  </code></pre>
  
  <p>But I was wrong, see ErikH's answer: <a href="https://github.com/civicrm/civicrm-core/blob/master/CRM/Core/DAO.php#L958" rel="nofollow noreferrer"><code>CRM_Core_DAO::checkTableExists()</code></a>!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T10:45:20.007Z
Answers.LastActivityDate: 2017-02-27T19:01:38.133Z
Answers.LastEditDate: 2017-02-27T19:01:38.133Z
Answers.UserDisplayName: Chris Burgess
Answers.Body: <p>If you know the name of the table, you could use MySQL via command line:</p>
  
  <p>Log into MySQL using terminal and then execute a command:</p>
  
  <p><code>mysql -u [username] -p</code></p>
  
  <p>mysql> <code>show tables like "test1";</code></p>
  
  <p>If it exists, you will get an output such as:</p>
  
  <pre><code>+------------------------+
  | Tables_in_test (test1) |
  +------------------------+
  | test1                  |
  +------------------------+
  1 row in set (0.00 sec)
  </code></pre>
  
  <p><a href="https://www.electrictoolbox.com/check-if-mysql-table-exists/" rel="nofollow noreferrer">https://www.electrictoolbox.com/check-if-mysql-table-exists/</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-15T20:32:32.520Z
Answers.Text: I need to do it from my PHP script. I could probably execute that query using CRM_Core_DAO::executeQuery()
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-15T20:14:01.053Z
Answers.LastActivityDate: 2017-02-15T20:14:01.053Z
Answers.UserDisplayName: Christia
Answers.Body: <p>There is a specific CiviCRM function (but all the ways in the other answers will be just as good :-)):</p>
  
  <pre><code>CRM_Core_DAO::checkTableExists($tableName)
  </code></pre>
  
  <p>This function will return TRUE or FALSE.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T13:23:43.487Z
Answers.LastActivityDate: 2017-02-27T13:23:43.487Z
Answers.UserDisplayName: ErikH - CiviCooP
Body: <p>Is there a built-in way to check to see if a DB table exists via PHP? I'd like to add that to my API call and create the table if necessary.</p>
  
  <p>Thanks!</p>
  
Tags: <api><mysql>
Title: How to check if DB table exists?
ThreadText: # How to check if DB table exists?
  
  (Asked by: username_0 on 2017-02-15)
  
  Is there a built-in way to check to see if a DB table exists via PHP? I'd like to add that to my API call and create the table if necessary.
  
  Thanks!
  
  **Tags:** api, mysql
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_4 on 2017-02-27. Score: 3 | Accepted answer
  
  There is a specific CiviCRM function (but all the ways in the other answers will be just as good :-)):
  CRM_Core_DAO::checkTableExists($tableName)
  
  This function will return TRUE or FALSE.
  
  
  ---
  
  ### Answer by username_1 on 2017-02-15. Score: 2
  
  If you're writing PHP in a CiviCRM extension - you can add a /sql/install.sql file in which you can execute MySQL commands like:
  CREATE TABLE IF NOT EXISTS
  
  
  ---
  
  ### Answer by username_2 on 2017-02-27. Score: 2
  
  CRM_Queue_BAO_QueueItem:: findCreateTable() contains an example of how CiviCRM does this, and the presence of this suggested that no "canned" method might exist already.
  $checkTableSql = "show tables like 'civicrm_queue_item'";
  $foundName = CRM_Core_DAO::singleValueQuery($checkTableSql);
  if ($foundName == 'civicrm_queue_item') {
    return TRUE;
  }
  
  But I was wrong, see ErikH's answer: CRM_Core_DAO::checkTableExists()!
  
  
  ---
  
  ### Answer by username_3 on 2017-02-15. Score: 0
  
  If you know the name of the table, you could use MySQL via command line:
  Log into MySQL using terminal and then execute a command:
  mysql -u [username] -p
  mysql> show tables like "test1";
  If it exists, you will get an output such as:
  +------------------------+
  | Tables_in_test (test1) |
  +------------------------+
  | test1                  |
  +------------------------+
  1 row in set (0.00 sec)
  
  https://www.electrictoolbox.com/check-if-mysql-table-exists/
  
  > **Comments:**
  > **username_0**: I need to do it from my PHP script. I could probably execute that query using CRM_Core_DAO::executeQuery()  
  
  ---
  
  