Solved

Why is this table not being dropped?

Posted on 2014-10-27
6
103 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

749 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