PDA

View Full Version : PHP and SQL question




noelister
Jun 4, 2006, 03:45 PM
Hello,

I was hoping that someone on here could help me out. I am trying to write a little program that reads a form and then writes it to a DB. However, I would like to check to see if the table exist first if so use that table if not create the table.

kinda like

PSEUDO CODE

if(tableName == tableName){
SELECT TABLE
}else{
CREATE TABLE
}

I know I would have to loop through the DB to see what tables exist. I'm just unsure of the format and syntax.

Thanks for the help
N



Mitthrawnuruodo
Jun 4, 2006, 03:53 PM
What you should be able to do is use something like

CREATE TABLE IF NOT EXISTS 'tablename' (...)

but I've never done that from a PHP page, just when setting up new tables via phpMyAdmin (or command line mysql administrating).

Once it's made (or not if it existed), you can make querys against it...

noelister
Jun 4, 2006, 04:03 PM
Okay,

Is that the SQL statement to do that (CREATE TABLE IF NOT EXIST)? Like you said i usually create my DB and table with myphpadmin.

Mitthrawnuruodo
Jun 4, 2006, 04:13 PM
Yes, that's the mysql I usually use to make new tables via phpMyAdmin:

--
-- Table structure for table `files`
--

DROP TABLE IF EXISTS `files`;
CREATE TABLE IF NOT EXISTS `files` (
`id` int(11) NOT NULL auto_increment,
`parent` int(11) NOT NULL default '0',
`file` varchar(255) NOT NULL default '',
`thumb` varchar(255) NOT NULL default '',
`desc` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

You could try something like:

mysql_query("CREATE TABLE IF NOT EXISTS `files` (
`id` int(11) NOT NULL auto_increment,
`parent` int(11) NOT NULL default '0',
`file` varchar(255) NOT NULL default '',
`thumb` varchar(255) NOT NULL default '',
`desc` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
") or die ("Something's wrong!");

noelister
Jun 4, 2006, 04:24 PM
Okay, Thanks alot for the help.