when i removed the create table it is working, but is it possible to do this on code?
here is the code
$sql = "CREATE TABLE student (
student_id INT,
name VARCHAR(20),
major VARCHAR(20),
PRIMARY KEY(student_id)
)
INSERT INTO student VALUE(1, 'Jack', 'biology')";
if ($connect -> query($sql) === TRUE) {
echo "New Table Created! <br><br>";
}
else {
echo "Error : " . $sql . " <br><br>" . $connect -> error . "<br><br>";
}
echo "Connected Successfully!";
Here is the error when i didn't removed the create table
Error : CREATE TABLE student ( student_id INT, name VARCHAR(20), major VARCHAR(20), PRIMARY KEY(student_id) ); INSERT INTO student VALUE(2, 'kate', 'sociology')
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO student VALUE(2, 'kate', 'sociology')' at line 8
You have 2 typos in your code:
Missing ";" between the CREATE and INSERT statements.
INSERT statement uses VALUES, not VALUE.
$sql = "CREATE TABLE student (
student_id INT,
name VARCHAR(20),
major VARCHAR(20),
PRIMARY KEY(student_id)
);
INSERT INTO student VALUES (1, 'Jack', 'biology')";
Aside from that, check the official docs for mysqli Multiple statements and mysqli_multi_query() since as mentioned in the first document:
The API functions mysqli_query() and mysqli_real_query() do not set a connection flag necessary for activating multi queries in the server. An extra API call is used for multiple statements to reduce the likeliness of accidental SQL injection attacks.
Or you can also change your code to run each query individually.
I jist found out about multi_query($sql) i'm gonna try that later thanks to all who answer my question!
Please do not encourage the use of
mysqli_multi_query()
. Clearly, there is no reason to use it in this scenario.Okay thank you very much