mysql 1064 error SYNTAX ERROR - php

I keep getting this error every time i try to create a table in my sql database:
Error in query (1064): Syntax error near 'CREATE TABLE IF NOT EXISTS users ( id int(10) NOT NULL AUTO_INCREMENT, `' at line 2
Could I get some help with this?
use luke_f_db
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`trn_date` datetime NOT NULL,
PRIMARY KEY (`id`)
);

There's an error with your use command. Add an ; behind the first line or just check if the database exists and your account has access to it.
The CREATE Code worked fine for me.

You could run only one command per query**. The mySQL command line client allows you to split multiple commands with ; but still runs them sequentially.
Remove the use luke_f_db line. The database to be used should be selected within the connect command. See http://www.w3schools.com/php/func_mysqli_connect.asp:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
The database name is the last argument to mysqli_connect.
The same information is found in the official PHP documentation: http://php.net/manual/en/mysqli.construct.php
** Some mySQL client implementations also allow multiple commands in one call, but you should avoid it. Using this feature in a script isn't portable to other databases and you won't ever know which of your commands triggered an error if one occurs.

Related

How do i fix this problem ive tried alot?

I run into an SQL query error.
The query:
CREATE TABLE IF NOT EXISTS `shoutbox` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`user` varchar(255) NOT NULL,
`msg` varchar(255) NOT NULL,
`time` datetime(6) DEFAULT NOT NULL CURRENT_TIMESTAMP(6),
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
MySQL said:
#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
'(6) DEFAULT NOT NULL CURRENT_TIMESTAMP(6),
PRIMARY KEY (`id`)
) ENGINE=MyISAM ' at line 5
Since this looks like your first import.
MySQL reference manuals are per the link. As you can see 5.5 is so old its in a PDF version only.
Since you are importing a sql file, look closer at the text of it, it will say what version it came from. The first step should be to install the same major version of mysql (the first two digits like 5.7 or 8.0). If the last digit is later than the origin sql that's fine.
Don't start with 5.5, its too old. Consider a minimum of 5.7 first or 8.0 if the version of the sql dump is later.
Default keyword should be before the default value
`time` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
assuming the posted code has been transcribed accurately this amendment fixes the posted error...
AND what's the point of not null AND a default?

How to fix SQL error with dropping a table in PHP?

I have created a web you can upload and download files - everything works perfect. But now, I want to create a init file, that delete old records in database and create a new tables in it.
So I write this:
$command = "
IF OBJECT_ID(`".$database.".Users`) IS NOT NULL
DROP TABLE ".$database.".Users;
IF OBJECT_ID(`".$database.".Uploads`) IS NOT NULL
DROP TABLE ".$database.".Uploads;
CREATE TABLE `Users` (
`Id` int(11) NOT NULL,
`User` text NOT NULL,
`Password` text NOT NULL,
`Permission` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
ALTER TABLE `Users` ADD PRIMARY KEY (`Id`);
ALTER TABLE `Users` MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=0;
CREATE TABLE `Uploads` (
`Id` int(11) NOT NULL,
`Name` text NOT NULL,
`User` text NOT NULL,
`Comment` text NOT NULL,
`Path` text NOT NULL,
`Permission` int(11) NOT NULL,
`Date` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
ALTER TABLE `Uploads` ADD PRIMARY KEY (`Id`);
ALTER TABLE `Uploads` MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=0;
";
$result = mysqli_query($conn, $command) or die(mysqli_error($conn));
I think, that code is right (but obviously not). When I run it, SQL throws an error:
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 'IF OBJECT_ID(db.Users) IS NOT NULL DROP TABLE db.User' at line 1**.
This means that it don’t have a problem with connection to SQL database.
I tried instead of IF OBJECT_ID use IF NOT EXISTS, but it doesn't works too. Can anybody tell me if multi-line SQL command is this problem or if it is something else?
Note: I use 5.5.37 version of MariaDB (if it helps)
IF is not a valid SQL statement in MySQL / MariaDB.
The IF OBJECT_ID(...) statement in the question appears to be a Transact-SQL (Microsoft SQL Server) construct.
The equivalent functionality in MySQL would be achieved with
DROP TABLE IF EXISTS foo.mytable ;
(I expect this would work in MariaDB 5.5, but I haven't verified.)
Note that if the table doesn't exist, the execution of the statement will raise a warning. (A warning message, not an error message.)
The mysqli_query function runs a single statement. To run multiple statements, we can use mysqli_multi_query function, documented here:
http://php.net/manual/en/mysqli.multi-query.php
As far as concerns, OBJECT_ID does not exist in mysql, only in mssql. Searching for OBJECT_ID mysql 8.0 reference manual does not retun anything meaningful. Even if it existed, your syntax for IF block does not look good : you want IF...THEN...END.
To fix the error, you can replace this :
IF OBJECT_ID(`".$database.".Users`) IS NOT NULL
DROP TABLE ".$database.".Users;
IF OBJECT_ID(`".$database.".Uploads`) IS NOT NULL
DROP TABLE ".$database.".Uploads;
With :
DROP TABLE IF EXISTS ".$database.".Users;
DROP TABLE IF EXISTS ".$database.".Uploads;
never used OBJECT_ID but what you want seem to be easily doable with
"drop table if exists users;"

getting syntax when creating table [duplicate]

This question already has answers here:
How to execute two mysql queries as one in PHP/MYSQL?
(8 answers)
Closed 6 years ago.
I am trying to create a mySQL table with php and I am getting a syntax error whenever I run my code. The string is
$sql = "DROP TABLE Colors;
CREATE TABLE Colors (
color VARCHAR(30) NOT NULL,
vote INT(16) NOT NULL UNSIGNED,
hex VARCHAR(10) NOT NULL,
PRIMARY KEY (color)
)";
query line is ($conn is the connection to the server)
$conn->query($sql)
The error is
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 Colors (
color VARCHAR(30) NOT NULL,
vote INT(16) NOT NULL UNSIGNED' at line 2
Does anyone see what I am doing wrong?
You need to execute one statement at a time. ; is a construct used by the command-line tool as a delimiter, it's not actually used in the MySQL protocol for communicating so it's an error. The DELIMITER value can also be changed.
Split up it looks like this:
$conn->query("DROP TABLE Colors");
$conn->query("CREATE TABLE ...");
Note, I strongly encourage you to do proper error checking here and have exceptions turned on so that if a statement fails you know about it.
Not sure which mysql lib you are using, but in PHP you should use function multi_query in your scenario.
$sql = "DROP TABLE Colors;
CREATE TABLE Colors (
color VARCHAR(30) NOT NULL,
vote INT(16) NOT NULL UNSIGNED,
hex VARCHAR(10) NOT NULL,
PRIMARY KEY (color)
)";
mysqli_multi_query($conn, $sql);

Is it possible to run a long .sql file through Codeigniter?

I want to run a .sql file from my server in Codeigniter 3.1.0. I have tried following
//code to create a DB & this is successful then following ocde
$query = file_get_contents('./test.sql');
$this->db->query($query);
Here is my test.sql file. https://gist.github.com/rejoan/97dfae1b08116e386b3e6fda97eeb4f7
Now when I run this it shows error always
A Database Error Occurred
Error Number: 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 user ( id int(11) NOT NULL, test_id int(11)
NOT NULL, `' at line 10
CREATE TABLE `test`
( `id` int(11) NOT NULL,
`name` varchar(250) NOT NULL,
`description` text NOT NULL,
`added` date NOT NULL,
`outdate` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `user`
( `id` int(11) NOT NULL,
`test_id` int(11) NOT NULL,
`name` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `test` ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `name` (`name`);
ALTER TABLE `user` ADD PRIMARY KEY (`id`),
ADD KEY `test_id` (`test_id`);
ALTER TABLE `test` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=37;
ALTER TABLE `user` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `user`
ADD CONSTRAINT `FK_user_test` FOREIGN KEY (`test_id`)
REFERENCES `test` (`id`) ON UPDATE NO ACTION;
Filename: C:/xampp/htdocs/spider_clients/system/database/DB_driver.php
Line Number: 691
So how can I resolve this by issue? I have a idea to do this by PHP raw code but Firstly I want to solve by CI. Any Idea?
** Even I have tried by replacing all backtick by single quote still not works
You can't run any SQL file through the API. Not even if you split up the file and run one query per API call.
There are some commands that can appear in SQL files, but they're actually mysql client builtin commands. These commands are not recognized by the SQL parser in the server.
It's tricky to split up the SQL file. There are SQL statements that contain literal semicolons, like CREATE TRIGGER. So you need more complex logic to split up the file, it's not as simple as preg_split('/;/', $query)
Another gotcha: you'll find that submitting a "query" that consists of nothing but an SQL comment causes an error.
Also, if your SQL file is too large, you'll blow out PHP's max memory if you use file_get_contents().
The bottom line is that you'll waste a lot of your time developing this code and trying to make it work. You're better off leveraging the tool that is already designed to run SQL scripts:
shell_exec("mysql databasename < ./test.sql");
See also:
Running MySQL *.sql files in PHP

SQLite error on table creation with index

I am trying to make SQLite3 database with PHP that has an index.
There is an example on dev.mysql that makes an index like the one below.
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE sales (
kitchen_name VARCHAR(255) NOT NULL,
amount_name VARCHAR(255),
amount NUMERIC(15,2),
name_key VARCHAR(255) NOT NULL,
flag_total TINYINT(1),
INDEX kitchen_value_key (kitchen_name,name_key)
);");
I have run the query in an online parser and it is coming back as valid MySQL.
But I keep getting the error:
SQLite3::exec(): near "INDEX": syntax error
What am I doing wrong?
This is indeed valid MySQL syntax, but you aren't using MySQL, you're using SQLite, which is a different RDBMS.
SQLite just doesn't support the syntax for inlining index definitions in the table definitions, so you'll have to resort to using two separate statements:
CREATE TABLE sales (
kitchen_name VARCHAR(255) NOT NULL,
amount_name VARCHAR(255),
amount NUMERIC(15,2),
name_key VARCHAR(255) NOT NULL,
flag_total TINYINT(1)
);
CREATE INDEX kitchen_value_key ON sales(kitchen_name,name_key);
SQLFiddle
SQLITE is not mysql. They have very different syntactic rules at points
I'm assuming you want to create a normal index on the SQLITE table.
Put the index creation in a seperate statement from the table creation.
You should follow the instructions on
https://www.sqlite.org/lang_createindex.html
CREATE INDEX IF NOT EXISTS kitchen_value_key ON sales (kitchen_name, name_key)

Categories