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)
php mysql sql mariadb
add a comment |
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)
php mysql sql mariadb
There are many places where you used single quotes'
instead of ticks`
.
– Funk Forty Niner
Jan 2 at 18:21
Yeah, but I tried it even with ticks, but result was same.
– Hackrrr
Jan 2 at 18:23
Still; you're going to have to update your question contain the real syntax for all this. It could be closed based on that also.
– Funk Forty Niner
Jan 2 at 18:23
1
You need to delimit table and field names with ` instead of '; and they must be delimited separately...maybe, I've never seen OBJECT_ID used in Mysql/MariaDB, only MSSQL. Also, unless MariaDB has drastically diverged from MySQL, you cannot execute scripts as queries. You need to execute those one at a time, and useDROP TABLE IF EXISTS
since you cannot useIF
.
– Uueerdo
Jan 2 at 18:27
Sorry - Now it is right.
– Hackrrr
Jan 2 at 18:27
add a comment |
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)
php mysql sql mariadb
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)
php mysql sql mariadb
php mysql sql mariadb
edited Jan 2 at 19:01
GMB
18.4k31028
18.4k31028
asked Jan 2 at 18:11
HackrrrHackrrr
168212
168212
There are many places where you used single quotes'
instead of ticks`
.
– Funk Forty Niner
Jan 2 at 18:21
Yeah, but I tried it even with ticks, but result was same.
– Hackrrr
Jan 2 at 18:23
Still; you're going to have to update your question contain the real syntax for all this. It could be closed based on that also.
– Funk Forty Niner
Jan 2 at 18:23
1
You need to delimit table and field names with ` instead of '; and they must be delimited separately...maybe, I've never seen OBJECT_ID used in Mysql/MariaDB, only MSSQL. Also, unless MariaDB has drastically diverged from MySQL, you cannot execute scripts as queries. You need to execute those one at a time, and useDROP TABLE IF EXISTS
since you cannot useIF
.
– Uueerdo
Jan 2 at 18:27
Sorry - Now it is right.
– Hackrrr
Jan 2 at 18:27
add a comment |
There are many places where you used single quotes'
instead of ticks`
.
– Funk Forty Niner
Jan 2 at 18:21
Yeah, but I tried it even with ticks, but result was same.
– Hackrrr
Jan 2 at 18:23
Still; you're going to have to update your question contain the real syntax for all this. It could be closed based on that also.
– Funk Forty Niner
Jan 2 at 18:23
1
You need to delimit table and field names with ` instead of '; and they must be delimited separately...maybe, I've never seen OBJECT_ID used in Mysql/MariaDB, only MSSQL. Also, unless MariaDB has drastically diverged from MySQL, you cannot execute scripts as queries. You need to execute those one at a time, and useDROP TABLE IF EXISTS
since you cannot useIF
.
– Uueerdo
Jan 2 at 18:27
Sorry - Now it is right.
– Hackrrr
Jan 2 at 18:27
There are many places where you used single quotes
'
instead of ticks `
.– Funk Forty Niner
Jan 2 at 18:21
There are many places where you used single quotes
'
instead of ticks `
.– Funk Forty Niner
Jan 2 at 18:21
Yeah, but I tried it even with ticks, but result was same.
– Hackrrr
Jan 2 at 18:23
Yeah, but I tried it even with ticks, but result was same.
– Hackrrr
Jan 2 at 18:23
Still; you're going to have to update your question contain the real syntax for all this. It could be closed based on that also.
– Funk Forty Niner
Jan 2 at 18:23
Still; you're going to have to update your question contain the real syntax for all this. It could be closed based on that also.
– Funk Forty Niner
Jan 2 at 18:23
1
1
You need to delimit table and field names with ` instead of '; and they must be delimited separately...maybe, I've never seen OBJECT_ID used in Mysql/MariaDB, only MSSQL. Also, unless MariaDB has drastically diverged from MySQL, you cannot execute scripts as queries. You need to execute those one at a time, and use
DROP TABLE IF EXISTS
since you cannot use IF
.– Uueerdo
Jan 2 at 18:27
You need to delimit table and field names with ` instead of '; and they must be delimited separately...maybe, I've never seen OBJECT_ID used in Mysql/MariaDB, only MSSQL. Also, unless MariaDB has drastically diverged from MySQL, you cannot execute scripts as queries. You need to execute those one at a time, and use
DROP TABLE IF EXISTS
since you cannot use IF
.– Uueerdo
Jan 2 at 18:27
Sorry - Now it is right.
– Hackrrr
Jan 2 at 18:27
Sorry - Now it is right.
– Hackrrr
Jan 2 at 18:27
add a comment |
3 Answers
3
active
oldest
votes
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
mysqli_multi_query - That it is! Thanks!
– Hackrrr
Jan 2 at 19:54
Avoidmysqli_multi_query
for security reasons. Either issue multiple commands from PHP (not a problem), or write a Stored Procedure to encapsulate it all together, thenCALL
the SP.
– Rick James
Jan 2 at 22:57
add a comment |
never used OBJECT_ID but what you want seem to be easily doable with
"drop table if exists users;"
I wrote it - I tried it too.
– Hackrrr
Jan 2 at 18:24
oh sorry i misunderstood. you wrote - "I tried instead of IF OBJECT_ID use IF NOT EXISTS" i thought you tried writing 'IF not EXIST' in create table query not in drop table query
– Sourabh Swarnkar
Jan 2 at 18:31
add a comment |
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;
I wrote, that I tried it, but not works. (Now, I tried it again, but it doesn't works again)
– Hackrrr
Jan 2 at 19:33
@Hackrrr : which error are you getting exactly ?
– GMB
Jan 2 at 19:39
@GBM After change "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 'DROP TABLE IF EXISTS db.Uploads; CREATE TABLEUsers
(Id
' at line 2" (Nearly same)
– Hackrrr
Jan 2 at 19:48
Don't issue multiple commands in the same call.
– Rick James
Jan 2 at 22:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011176%2fhow-to-fix-sql-error-with-dropping-a-table-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
mysqli_multi_query - That it is! Thanks!
– Hackrrr
Jan 2 at 19:54
Avoidmysqli_multi_query
for security reasons. Either issue multiple commands from PHP (not a problem), or write a Stored Procedure to encapsulate it all together, thenCALL
the SP.
– Rick James
Jan 2 at 22:57
add a comment |
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
mysqli_multi_query - That it is! Thanks!
– Hackrrr
Jan 2 at 19:54
Avoidmysqli_multi_query
for security reasons. Either issue multiple commands from PHP (not a problem), or write a Stored Procedure to encapsulate it all together, thenCALL
the SP.
– Rick James
Jan 2 at 22:57
add a comment |
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
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
answered Jan 2 at 19:48
spencer7593spencer7593
85.9k118197
85.9k118197
mysqli_multi_query - That it is! Thanks!
– Hackrrr
Jan 2 at 19:54
Avoidmysqli_multi_query
for security reasons. Either issue multiple commands from PHP (not a problem), or write a Stored Procedure to encapsulate it all together, thenCALL
the SP.
– Rick James
Jan 2 at 22:57
add a comment |
mysqli_multi_query - That it is! Thanks!
– Hackrrr
Jan 2 at 19:54
Avoidmysqli_multi_query
for security reasons. Either issue multiple commands from PHP (not a problem), or write a Stored Procedure to encapsulate it all together, thenCALL
the SP.
– Rick James
Jan 2 at 22:57
mysqli_multi_query - That it is! Thanks!
– Hackrrr
Jan 2 at 19:54
mysqli_multi_query - That it is! Thanks!
– Hackrrr
Jan 2 at 19:54
Avoid
mysqli_multi_query
for security reasons. Either issue multiple commands from PHP (not a problem), or write a Stored Procedure to encapsulate it all together, then CALL
the SP.– Rick James
Jan 2 at 22:57
Avoid
mysqli_multi_query
for security reasons. Either issue multiple commands from PHP (not a problem), or write a Stored Procedure to encapsulate it all together, then CALL
the SP.– Rick James
Jan 2 at 22:57
add a comment |
never used OBJECT_ID but what you want seem to be easily doable with
"drop table if exists users;"
I wrote it - I tried it too.
– Hackrrr
Jan 2 at 18:24
oh sorry i misunderstood. you wrote - "I tried instead of IF OBJECT_ID use IF NOT EXISTS" i thought you tried writing 'IF not EXIST' in create table query not in drop table query
– Sourabh Swarnkar
Jan 2 at 18:31
add a comment |
never used OBJECT_ID but what you want seem to be easily doable with
"drop table if exists users;"
I wrote it - I tried it too.
– Hackrrr
Jan 2 at 18:24
oh sorry i misunderstood. you wrote - "I tried instead of IF OBJECT_ID use IF NOT EXISTS" i thought you tried writing 'IF not EXIST' in create table query not in drop table query
– Sourabh Swarnkar
Jan 2 at 18:31
add a comment |
never used OBJECT_ID but what you want seem to be easily doable with
"drop table if exists users;"
never used OBJECT_ID but what you want seem to be easily doable with
"drop table if exists users;"
answered Jan 2 at 18:22
Sourabh SwarnkarSourabh Swarnkar
58113
58113
I wrote it - I tried it too.
– Hackrrr
Jan 2 at 18:24
oh sorry i misunderstood. you wrote - "I tried instead of IF OBJECT_ID use IF NOT EXISTS" i thought you tried writing 'IF not EXIST' in create table query not in drop table query
– Sourabh Swarnkar
Jan 2 at 18:31
add a comment |
I wrote it - I tried it too.
– Hackrrr
Jan 2 at 18:24
oh sorry i misunderstood. you wrote - "I tried instead of IF OBJECT_ID use IF NOT EXISTS" i thought you tried writing 'IF not EXIST' in create table query not in drop table query
– Sourabh Swarnkar
Jan 2 at 18:31
I wrote it - I tried it too.
– Hackrrr
Jan 2 at 18:24
I wrote it - I tried it too.
– Hackrrr
Jan 2 at 18:24
oh sorry i misunderstood. you wrote - "I tried instead of IF OBJECT_ID use IF NOT EXISTS" i thought you tried writing 'IF not EXIST' in create table query not in drop table query
– Sourabh Swarnkar
Jan 2 at 18:31
oh sorry i misunderstood. you wrote - "I tried instead of IF OBJECT_ID use IF NOT EXISTS" i thought you tried writing 'IF not EXIST' in create table query not in drop table query
– Sourabh Swarnkar
Jan 2 at 18:31
add a comment |
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;
I wrote, that I tried it, but not works. (Now, I tried it again, but it doesn't works again)
– Hackrrr
Jan 2 at 19:33
@Hackrrr : which error are you getting exactly ?
– GMB
Jan 2 at 19:39
@GBM After change "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 'DROP TABLE IF EXISTS db.Uploads; CREATE TABLEUsers
(Id
' at line 2" (Nearly same)
– Hackrrr
Jan 2 at 19:48
Don't issue multiple commands in the same call.
– Rick James
Jan 2 at 22:59
add a comment |
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;
I wrote, that I tried it, but not works. (Now, I tried it again, but it doesn't works again)
– Hackrrr
Jan 2 at 19:33
@Hackrrr : which error are you getting exactly ?
– GMB
Jan 2 at 19:39
@GBM After change "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 'DROP TABLE IF EXISTS db.Uploads; CREATE TABLEUsers
(Id
' at line 2" (Nearly same)
– Hackrrr
Jan 2 at 19:48
Don't issue multiple commands in the same call.
– Rick James
Jan 2 at 22:59
add a comment |
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;
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;
answered Jan 2 at 19:14
GMBGMB
18.4k31028
18.4k31028
I wrote, that I tried it, but not works. (Now, I tried it again, but it doesn't works again)
– Hackrrr
Jan 2 at 19:33
@Hackrrr : which error are you getting exactly ?
– GMB
Jan 2 at 19:39
@GBM After change "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 'DROP TABLE IF EXISTS db.Uploads; CREATE TABLEUsers
(Id
' at line 2" (Nearly same)
– Hackrrr
Jan 2 at 19:48
Don't issue multiple commands in the same call.
– Rick James
Jan 2 at 22:59
add a comment |
I wrote, that I tried it, but not works. (Now, I tried it again, but it doesn't works again)
– Hackrrr
Jan 2 at 19:33
@Hackrrr : which error are you getting exactly ?
– GMB
Jan 2 at 19:39
@GBM After change "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 'DROP TABLE IF EXISTS db.Uploads; CREATE TABLEUsers
(Id
' at line 2" (Nearly same)
– Hackrrr
Jan 2 at 19:48
Don't issue multiple commands in the same call.
– Rick James
Jan 2 at 22:59
I wrote, that I tried it, but not works. (Now, I tried it again, but it doesn't works again)
– Hackrrr
Jan 2 at 19:33
I wrote, that I tried it, but not works. (Now, I tried it again, but it doesn't works again)
– Hackrrr
Jan 2 at 19:33
@Hackrrr : which error are you getting exactly ?
– GMB
Jan 2 at 19:39
@Hackrrr : which error are you getting exactly ?
– GMB
Jan 2 at 19:39
@GBM After change "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 'DROP TABLE IF EXISTS db.Uploads; CREATE TABLE
Users
( Id
' at line 2" (Nearly same)– Hackrrr
Jan 2 at 19:48
@GBM After change "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 'DROP TABLE IF EXISTS db.Uploads; CREATE TABLE
Users
( Id
' at line 2" (Nearly same)– Hackrrr
Jan 2 at 19:48
Don't issue multiple commands in the same call.
– Rick James
Jan 2 at 22:59
Don't issue multiple commands in the same call.
– Rick James
Jan 2 at 22:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011176%2fhow-to-fix-sql-error-with-dropping-a-table-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There are many places where you used single quotes
'
instead of ticks`
.– Funk Forty Niner
Jan 2 at 18:21
Yeah, but I tried it even with ticks, but result was same.
– Hackrrr
Jan 2 at 18:23
Still; you're going to have to update your question contain the real syntax for all this. It could be closed based on that also.
– Funk Forty Niner
Jan 2 at 18:23
1
You need to delimit table and field names with ` instead of '; and they must be delimited separately...maybe, I've never seen OBJECT_ID used in Mysql/MariaDB, only MSSQL. Also, unless MariaDB has drastically diverged from MySQL, you cannot execute scripts as queries. You need to execute those one at a time, and use
DROP TABLE IF EXISTS
since you cannot useIF
.– Uueerdo
Jan 2 at 18:27
Sorry - Now it is right.
– Hackrrr
Jan 2 at 18:27