Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:35:58+00:00 2026-06-12T19:35:58+00:00

I am trying to create a table in MySQL based on user input. Ideally,

  • 0

I am trying to create a table in MySQL based on user input. Ideally, the Perl script would connect to the database and create a table with a variable that was received from the user. Here’s my script:

print "Please enter a name for the table: ";
$tableName = <>;
chomp($tableName);
&createTable ($tableName);

sub createTable
{
    use DBI;
    my $platform = "mysql";
    my $database = "example";
    my $host = "localhost";
    my $user = "user";
    my $pw = "pw";
    my $dsn = "dbi:$platform:$database:$host";
    my $dbh = DBI->connect($dsn, $user, $pw) or die "Unable to connect: $DBI::errstr\n";
    $dbh->do("DROP TABLE IF EXISTS $_");
    $dbh->do("CREATE TABLE $table (column VARCHAR(17))");
    $dbh->disconnect;
}

But when I execute the script, and enter a value (let’s say ‘test’), it blurts this back to me:

DBD::mysql::db do failed: 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 at /path/to/scripts/dbTest.pl line 28, <> line 2.
DBD::mysql::db do failed: 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 '(column VARCHAR(17))' at line 1 at /path/to/scripts/dbTest.pl line 29, <> line 2.

Line 28 is the DROP command, and line 29 is the CREATE command.
I’ve checked my syntax plenty of times, but I’m not seeming to get where the error is at. Am I overlooking something so simple..?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:35:59+00:00Added an answer on June 12, 2026 at 7:35 pm

    Try that :

    use warnings; use strict;
    use DBI;
    print "Please enter a name for the table: ";
    $tableName = <>;
    chomp($tableName);
    createTable($tableName);
    
    sub createTable {
        my $table = shift;
    
        my $platform = "mysql";
        my $database = "example";
        my $host = "localhost";
        my $user = "user";
        my $pw = "pw";
        my $dsn = "dbi:$platform:$database:$host";
        my $dbh = DBI->connect($dsn, $user, $pw)
            or die "Unable to connect: $DBI::errstr\n";
        $dbh->do("DROP TABLE IF EXISTS $table");
        $dbh->do("CREATE TABLE $table (column VARCHAR(17))"); 
        $dbh->disconnect;
    }
    

    You can’t use $_ like that in your functions. You have to deal with @_ instead (or use shift like I does). See perldoc perlsub

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create this table in a MySQL database CREATE TABLE IF
I'm trying to create a script to normalize another table in MySQL. Below is
I'm trying to create a table in php that would show the data on
I'm trying to create a simple class based on my database PDO class that
I am trying to create a table based on the Magento database structure. I
I am trying to create a table in MySql using php. My code looks
I'm trying to create a mysql table from the inner join between two other
I'm trying to convert my MySQL create table statements to SQLite create table statements.
I am trying to write a PHP script that will create an HTML form
I am trying to create a Trigger for a table that UDATES. I thought

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.