We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11413
    • 203 Posts
    Hi,

    I’m gonna use the DBAPI extensively in my module and I wanted to know if there was an easy way integrated to it to find if a table already exists and if not create it. I want the module to install itself on first run.

    thanks in advance,

    Blaise
      Blaise Bernier

      www.medialdesign.com - Solutions for small business, hosting, design and more!
    • Best approach is something like this (minus the PHP tags of course)
      ;)

      <?php
      // define function to auto-generate table
      if (!function_exists('checkSamplesTable')) {
      	/**
      	 * Auto-creates the data table if it does not exist
      	 */
      	function checkSamplesTable($samplesTable, & $errorMsg) {
      		global $modx;
      		$sql= "CREATE TABLE IF NOT EXISTS `{$samplesTable}` (" .
      				"`id` int(11) NOT NULL auto_increment, " .
      				"`name` varchar(255) default NULL, " .
      				"`disabled` binary(1) NOT NULL default '0', " .
      				"`datetime` datetime default NULL, PRIMARY KEY  (`id`)" .
      				")";
      		if (!$modx->db->query($sql)) {
      			$errorMsg.= "<p class=\"error\">Error creating table {$samplesTable}!</p>\n";
      		}
      	}
      }
      ?>
      
        • 11413
        • 203 Posts
        Nice! I didn’t knew that there was something like that in SQL!

        Btw... integrating phpMyAdmin to MODx would be a REALLY nice administration feature laugh

        thanks a lot!

        Blaise
          Blaise Bernier

          www.medialdesign.com - Solutions for small business, hosting, design and more!