Jump to content
ludo1960

Create Table if not exists

Recommended Posts

I have the following code, The table 'TEST' is not created but the code reports "Table has been created" Any idea what is wrong?

<?php
 
$conn = mysqli_connect("localhost","myuser","mypassword","mydatabase");
 
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
 
$create_table = "CREATE TABLE IF NOT EXIST `TEST`
(
ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
County VARCHAR(50) NOT NULL,
Country VARCHAR(50) NOT NULL
)";
 
$create_tbl = $conn->query($create_table);
 
if ($create_table)
{
echo "Table has been created";
}
else
{
echo "error!!";
}
Edited by ludo1960

Share this post


Link to post
Share on other sites

Of course it tells you it has been created. You are creating a atring variable then checking if there is a value in it.

To check creation of the table you need to check the query result - $create_tbl

And it should be "EXISTS", not "EXIST"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.