1

I'm not sure what's happening here but I cannot seem to create the table.

Is this a syntax error or something else?

When I tried to paste the CREATE TABLE part into the SQL part on PHPMyAdmin, I had to tinker with the syntax a bit before it worked.

What I want to be able to do it via PHP directly.

$server = 'localhost';
$user = 'root';
$pass = '';

$conn = mysqli_connect($server, $user, $pass);

if (! $conn) {
    echo 'Failed to connect to Server';
}
else {
    echo 'Connected';
}

$sql = 'CREATE DATABASE college';
$table = 'CREATE TABLE students(
    student_id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    student_name VARCHAR(255) NOT NULL,
    student_email VARCHAR(255) NOT NULL,
    student_city VARCHAR(255) NOT NULL,
)';

if (mysqli_query($conn, $sql)) {
    echo 'Database created';
}
else {
    echo 'Failed to create Database';
}

if (mysqli_query($conn, $table)) {
    echo 'Table Created';
}
else {
    echo 'Failed to create Table';
}
2
  • 3
    I think you have to select the database you are wanting to create the table in. Between your "create database" and your "create table", you have to make something like mysqli_select_db($conn,'college'); Commented Jul 15, 2016 at 14:11
  • 2
    You're not selecting DB Commented Jul 15, 2016 at 14:11

3 Answers 3

1

After create database successfully to need to select database then use create statement.

mysqli_select_db($conn, 'college'); // select database first

if (mysqli_query($conn, $table)) {
    echo 'Table Created';
}
else {
    echo 'Failed to create Table';
}
Sign up to request clarification or add additional context in comments.

Comments

1

Remove the comma in the end of the below line inside the CREATE TABLE

student_city VARCHAR(255) NOT NULL, 

it will cause the error below:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1

Comments

-2

I struggled with this a bit, especially because defining $sql = (create table query...) isn't enough, at least with MariaDB, despite what many tutorials suggest. It doesn't throw an error, but it also doesn't create anything.

I solved it by adding this line after defining $sql:

mysqli_query($con, $sql);

5 Comments

It should throw an exception by default in current PHP versions, make you sure you enable error reporting in your local development box. In older versions (or if exception is disabled) you need to check for error messages with mysqli_error().
In any case, this is not an answer to this question. Code in the question already executes the code, and what kind of tutorial states that storing SQL in a variable automatically runs it, a tutorial written by ChatGPT?
no chatgpt for sure, only tried on my machine and it works only with adding " mysqli_query" etc. tried what the other users wrote, and it didnt work. So, could you try to explain this instead of citing chatgpt, which could also sounds offensive to someone? thank you very very much.
I'm sorry if my previous comments fail to explain something, but I honestly can't tell what can possibly be missing. $sql = 'Please do thing X'; does not cause PHP to do thing X. What it does it to store a text in a variable, and nothing else. Your claim that there're many tutorials saying otherwise is maybe a bit too bold.
oh ok... all clear. thank you very much. i keep , nevertheless, to omit the line i inserted...and it does not create any table....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.