Needing help with making SQL code w/variables in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rob.weaver
Forum Newbie
Posts: 8
Joined: Tue Apr 22, 2003 11:18 am
Location: Houston, TX USA

Needing help with making SQL code w/variables in PHP

Post by rob.weaver »

Here is a summary

I built an app that is "customizable" to the user. The user can select their database (enter database, username, password), table prefix desired, and many other features that are stored as variables in PHP.

The app is complete and I am now working on a universal installer. The SQL code is for MySQL server (future versions will support many different alternatives) and is about 300 lines so I'm trying to find a simple find/replace strategy for the code.

Does anyone have any suggestions on how to accomplish this?

Thanks ahead of time!

Rob
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you could include a file containing the statments after setting the parameters, so that variables are substitued in string literals, e.g.

Code: Select all

<?php // file: setup.mysql.php
$sql = <<<EOD
CREATE TABLE {$tableprefix}categories (
  cat_id mediumint( 8 ) unsigned NOT NULL auto_increment,
  cat_title varchar(100) default NULL,
  cat_order mediumint( 8 ) unsigned NOT NULL default '0',
  PRIMARY KEY  (cat_id),
  KEY cat_order (cat_order)
) TYPE=MyISAM;

CREATE TABLE {$tableprefix}config (
  config_name varchar(255) NOT NULL default '',
  config_value varchar(255) NOT NULL default '',
  PRIMARY KEY  (config_name)
) TYPE=MyISAM;
EOD;
?>

Code: Select all

<html>
	<body>
<?php
$tableprefix = 'mytbl_';
require('setup.mysql.php');
$sql = explode(";", $sql);
foreach($sql as $stat)
{
?>
		<fieldset>
			<pre><?php echo $stat; ?></pre>
		</fieldset>
<?php	
}
?>
	</body>
</html>
Post Reply