top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to create a SQL table if table does not exists using php?

+1 vote
378 views

I am tring to create a new table into the database if the table name given is not present in database using Php .
This is the code that I am using .

  $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(!$conn)
    {
            die('Failed to connect to server: ' . mysql_error());
            exit;
    }
    $db_selected = mysql_select_db($dbname);
    if(!$db_selected)
    {
           $db_selected = " CREATE DATABASE $dbname ";
    }
    $usertb1 = mysql_query("select 1 from $usertb LIMIT 1");
    if( $usertb1 !== FALSE )
    {
           $sql = "CREATE TABLE IF NOT EXISTS $usertb ( uid INT(20) AUTO_INCREMENT UNIQUE KEY NOT NULL,name VARCHAR(40),email varchar(40) PRIMARY KEY NOT NULL)";
    }
    $typetb1 = mysql_query("select 1 from $typetb LIMIT 1");
    if( $typetb1 !== FALSE)
    {
           $sql1 = "CREATE TABLE IF NOT EXISTS $typetb( uid INT(20) NOT NULL, type ENUM('WEB','APP','CART') NOT NULL ,unsubscribe TINYINT(1) NOT NULL,bounce TINYINT(1) NOT NULL,complaint TINYINT(1) NOT NULL, PRIMARY KEY (uid,type), FOREIGN KEY(uid) WHERE promo_user(uid))";
    }
posted May 18, 2017 by Ajay Shetty

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
0 votes
for($i=0;$i<=feof($getdata);$i++)
{
if (filter_var($data[$i][1], FILTER_VALIDATE_EMAIL)){
echo $data[$i][1];
$email=$data[$i][1];
$conn = mysqli_connect($dbhost,$dbuser,$dbpass, $dbname);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$sql ="INSERT INTO promo_user (uid,name,email) VALUES (,'', '$email')";
mysqli_query($sql,$conn);
mysqli_close($conn);

I am using the above code but there is something wrong with it,whenever i run the code the echo is working fine but the content does go into sql table

Please help

0 votes

I have php script form which I used to input Members Details of my system into the MySQL database. My requirement is to display the last member's details in a input box.

+2 votes

I have a date field on an html form that users may leave blank. If they do leave it blank I want to write the date 01/01/1901 into the mysql table. How can I accomplish this and where in my .php script file should I put the code?

...