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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

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.