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";
};
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";
};
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
Related
All I want is to create 2 or more tables using tables.sql file via PHP.
PHP create_db.php
<?php
require 'config.php';
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
/* check connection */
if($mysqli->connect_errno){
echo "MySQL connection failed.<br>";
exit();
}else{
echo "MySQL successfully connected.<br>";
}
// DB create
if($mysqli->query('CREATE DATABASE IF NOT EXISTS '.DB_NAME.';') === TRUE){
echo "Database successfully created.<br>";
}else{
echo "Error: ".$mysqli->errno.", ".$mysqli->error."<br>";
}
// DB select
if($mysqli->select_db(DB_NAME) === TRUE){
echo "Database successfully selected.<br>";
}else{
echo "Error: ".$mysqli->errno.", ".$mysqli->error;
}
// Create tables
if($mysqli->query(file_get_contents('../sql/tables.sql')) === TRUE){
echo "Tables successfully created.<br>";
}else{
echo "Error: ".$mysqli->errno.", ".$mysqli->error."<br>";
}
$mysqli->close();
?>
DB_HOST, DB_USER, DB_PASSWORD, DB_NAME are defined in config.php file
PHP v7.4.7
SQL tables.sql
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `status` (
`status` varchar(100) NOT NULL,
`IDtime` varchar(100) NOT NULL
);
If I upload this tables.sql file directly to MySQL server using command line it is working.
MySQL v8.0.20 MySQL Comunity Server - GPL
Error message
Error: 1064, 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 'CREATE TABLE IF NOT EXISTS status ( status varchar(100) NOT NULL, `IDt' at line 16
mysqli::query only accepts single query https://www.php.net/manual/en/mysqli.query.php.
If you want to execute multiple queries at once you have to use mysqli::multi_query https://www.php.net/manual/en/mysqli.multi-query.php.
Example:
$result = $mysqli->query("SELECT * FROM reservations;SELECT * FROM reservations");
var_dump($result, $mysqli->error);
bool(false)
string(172) "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 'SELECT * FROM reservations' at line 1"
$result = $mysqli->multi_query("SELECT * FROM reservations;SELECT * FROM reservations");
var_dump($result, $mysqli->error);
bool(true)
string(0) ""
I am trying to use PHP to create a table in MySQL.
I already have made the database and PHP is connecting to MySQL just fine.
Except it gave me this error and it won't create the table in the database.
Here is my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CREATE TABLE Test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
testname VARCHAR(30) NOT NULL,
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
The error I am getting:
Error creating table: 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 ')' at line 4
Could someone please help me?
Remove the last comma , before the ) .
CREATE TABLE Test (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
testname VARCHAR(30) NOT NULL
)
Check your table DDL sql statement, there is an extra comma (,) before the closing parentheses in your sql.
I am creating MYSQLi table with PHP code but it returns syntax error.
<?php
require_once('../connect.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "CREATE TABLE email_list (
id INT AUTO_INCREMENT,
first_name varchar(20),
last_name varchar(20),
email varchar(60),
PRIMARY_KEY (id) );";
if (mysqli_query ($dbc, $query)) {
echo "The query was successfully executed!<br />";
} else {
echo "The query could not be executed!" . mysqli_error($dbc);
}
mysqli_close($dbc);
?>
Error:
The query could not be executed!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 '(id) )' at line 6
I think it is
PRIMARY KEY
Instead of
PRIMARY_KEY
This is my code http://prntscr.com/a2d8qq currently, I am learning things but I am really wondering why it will say that there is no database selected, tho I have selected it in line 5, also if I remove the "dbname = users_details" and then execute a query that creates a databse then it is fine. But whenever I create a table in that database (I selected it) it will not make me, I searched across google and it really is the same to my code but mine will not work.
<?php
try {
$connect = new PDO("mysql: host = 'localhost'; dbname = users_details", 'root', '');
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlQuery = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
$connect->exec($sqlQuery);
echo 'Successfully created table.';
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
So this fixed my problem: I have to execute a query to use a specified database into where I want my tables to be in. Then in the line 5 I have just removed the "dbname =
<?php
try {
$connect = new PDO("mysql: host = 'localhost';", 'root', '');
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlQuery = "CREATE TABLE details (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
$connect->exec("use users_details");
$connect->exec($sqlQuery);
echo 'Successfully created table.';
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
I am trying to learn using mysql in php. I started off trying to create a table in mysql, and using the mysqli extension.
My code:
<?php
$truemsg = "Table created successfully";
$falsemsg = "Error creating table: ";
$servername = "localhost";
$username = "myuser";
$password = "mypass";
$db = "mytable";
// Create database
$sql = "USE ".$db.";".
'CREATE TABLE IF NOT EXISTS Authentication (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
userid VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL,
role VARCHAR(20) NOT NULL,
email VARCHAR(50)
);';
print "Sql command is ".$sql;
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
print "<p></p>";
if ($conn->query($sql) === TRUE) {
echo $truemsg;
} else {
echo $falsemsg . $conn->error;
}
$conn->close();
?>
The error is:
Sql command is USE mytable;CREATE TABLE IF NOT EXISTS Authentication ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, userid VARCHAR(30) NOT NULL, password VARCHAR(30) NOT NULL, role VARCHAR(20) NOT NULL, email VARCHAR(50) );
Error creating table: 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 'CREATE TABLE IF NOT EXISTS Authentication ( id INT(6) UNSIGNED AUTO_INCREMENT PR' at line 1
I tried pasting the same command on the mysql command line, and it works fine. What's the problem using this in php?
You are supposed to run queries one by one
$sql = "query one";
$conn->query($sql);
$sql = 'query two';
$conn->query($sql);
instead of coupling them all in one statement.
DO NOT use mysqi_multi_query() either, this asynchronous function is not intended for the everyday use.
Also, in this particular case USE query is superfluous. Database should go into constructor:
$conn = new mysqli($servername, $username, $password, $db);
^^^ here
Also, tell mysqli to throw errors by itself, automatically, instead of checking result of every database command manually:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
This way you will get neat and clean code:
<?php
$servername = "localhost";
$username = "myuser";
$password = "mypass";
$db = "mytable";
// Create data table
$sql = 'CREATE TABLE IF NOT EXISTS Authentication (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
userid VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL,
role VARCHAR(20) NOT NULL,
email VARCHAR(50)
)';
// Create connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $db);
// Run a query
$conn->query($sql);
echo "Table created successfully";
This code will either report that table has been created successfully, or emit an error, with a detailed explanation on what went wrong.
This seems to be like a mysql multiple query problem
$conn->select_db($db);
you can use this function before the query to use the database and remove the use database statement from your query string , then you query string becomes
$sql = 'CREATE TABLE IF NOT EXISTS Authentication (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
userid VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL,
role VARCHAR(20) NOT NULL,
email VARCHAR(50)
)';
that may work for you ..