Hey, I'm working on a College project that means you have to re-do the database each time you work on your assignment. So, what I'm hoping to create is a file called "sql_connect.php" which will check if the table is there, if not.. It will run the function that creates it. Here is my code:

<?php
DEFINE ('DB_USER', 'USERNAME');
DEFINE ('DB_PASSWORD', 'PASSWORD');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'TABLE');

// make the connection
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('');

// select the database
$tbc = @mysql_select_db (DB_NAME) OR die ('fgddffdfdfd');

if(!$tbc)
{
  sql_insert();
}else{
}

?>

Thanks for any help :)

This should work...

mysql_query("CREATE TABLE IF NOT EXISTS `table_name` (
`field_name` int(11) NOT NULL AUTO_INCREMENT,
`field_name` varchar(65),
...,
...,
...,
PRIMARY KEY (`field_name`)
)");

Hey, I'm working on a College project that means you have to re-do the database each time you work on your assignment. So, what I'm hoping to create is a file called "sql_connect.php" which will check if the table is there, if not.. It will run the function that creates it. Here is my code:

<?php
DEFINE ('DB_USER', 'USERNAME');
DEFINE ('DB_PASSWORD', 'PASSWORD');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'TABLE');

// make the connection
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('');

// select the database
$tbc = @mysql_select_db (DB_NAME) OR die ('fgddffdfdfd');

if(!$tbc)
{
  sql_insert();
}else{
}

?>

Thanks for any help :)

<?php
//check table
function checktable($tablename){
return mysql_query("select * from ".$tablename." limit 0,1");
}

$table="table_name_that_want_to_be_check";
if(checktable($table)){
echo"table already created";
}else{
//create table if not exist
mysql_query("CREATE TABLE ".$table." (
         id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
         data VARCHAR(100), 
......................
       );")
echo "table created"
}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.