0

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 :)

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by forzadraco
1

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`)
)");

Edited by Will Gresham: n/a

-1

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"
}

?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.