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

  • SEARCH
  • Home
  • 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 8621449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:49:23+00:00 2026-06-12T06:49:23+00:00

I need to store the fetched data from the MySQL database. So I used

  • 0

I need to store the fetched data from the MySQL database. So I used this code.

while (@row = $statement->fetchrow_array) 
{
  print "@row.\n"; #        ------printing data
}

foreach $key(@row)
{
  print $key."\n"; # ------empty data
}

In foreach loop the @row data is empty. How to solve this

UPDATE: It actually should be like this:

while (my @row = $statement->fetchrow_array) { 
  # print "@row.\n";
  foreach my $key(@row) { 
    $query= "ALTER TABLE tablename DROP FOREIGN KEY $key;"; 
    $statement = $connection->prepare($query); 
    $statement->execute() 
      or die "SQL Error: $DBI::errstr\n"; 
  } 
}
  • 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-12T06:49:25+00:00Added an answer on June 12, 2026 at 6:49 am

    Well, it should be like this:

    while (my @row = $statement->fetchrow_array) {
      foreach my $key (@row) {
        print $key."\n";
      }
    }
    

    Otherwise all the result set will be consumed in the first loop.

    As a sidenote, fetchrow_array returns a rowset as array of field values, not keys. To get the keys as well, you should use fetchrow_hashref:

    while (my $row = $statement->fetchrow_hashref) {
      while (my ($key, $value) = each %$row) {
        print "$key => $value\n";
      }
    }
    

    UPDATE: from your comments it looks like you actually need an array of column names. There’s one way to do it:

    my $row = $statement->fetchrow_hashref;
    $statement->finish;
    foreach my $key (keys %$row) {
      # ... dropping $key from db as in your original example
    }
    

    But in fact, there are more convenient ways of doing what you want to do. First, there’s a single method for preparing and extracting a single row: selectrow_hashref. Second, if you want to get just foreign keys information, why don’t use the specific DBI method for that – foreign_key_info? For example:

    my $sth = $dbh->foreign_key_info( undef, undef, undef,
                                      undef, undef, $table_name);
    my $rows = $sth->fetchall_hashref;
    while (my ($k, $v) = each %$rows) {
      # process $k/$v
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to fetch data from database for backup in the form of insert
I need to assign a character array containing 300000 data which are fetched from
I need store just 10 arrays in my app, which I can change from
I need to store GPS coordinates in a database. I've heard that floats are
I need to store locally on android devices some images I get from the
I need to store lotto numbers in a SQL database. There are 8 separate
I need to store a large number of geocoordiantes in a database in a
I need to select a top row for each category from a known set
Here I have a MySQL database and I need to retrieve many rows. I
i am new to oracle.Already there is a store procedure which fetches data from

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.