Solved

Why is this table not being dropped?

Posted on 2014-10-27
6
112 Views
Last Modified: 2014-10-27
Need to drop a table. My code looks like this:

$the_twitter_table = "tw_2013-11-06";
$matt = "drop table $the_twitter_table";
$matt_query=mysqli_query($cxn, $matt);
if(!$matt_query)
{
$nuts=mysqli_errno($cxn).': '.mysqli_error($cxn);
die($nuts);
}

The error I get is "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 ''tw_2013-11-06'' at line 1"

When I run drop table tw_2013-11-06 in my phpMyAdmin interface, it works great, but I'm blowing it somewhere when I try to do it via PHP. What am I missing?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40406695
Put single quotes around the table name.

Instead of this:
$matt = "drop table $the_twitter_table";

Try this:
$matt = "DROP TABLE '$the_twitter_table' ";

Sidebar note: The capitalization is not necessary, but experienced programmers use it to make the SQL commands stand out, and it's often a programming standard in professionally run organizations.  Do whatever you think is right :-)
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40406701
Use backticks

$matt = "drop table `$the_twitter_table`";

Open in new window

0
 

Author Comment

by:brucegust
ID: 40406704
I tried that, Ray, and it still came up short. Same error. What else could it be?
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 58

Expert Comment

by:Gary
ID: 40406709
Ahem ^^^^^
0
 

Author Comment

by:brucegust
ID: 40406712
The backticks did it!

Thanks, Gary!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40406803
My apologies - I've never even considered putting a hyphen into a table or column name before.  Now that I'm testing this, I'm looking at your message:

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 ''tw_2013-11-06'' at line 1

... and wondering why it's different from the message that I get when I run your original query:

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 '-11-06' at line 1 QUERY: DROP TABLE tw_2013-11-06
0

Featured Post

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question