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 8560193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:13:18+00:00 2026-06-11T16:13:18+00:00

I am building a way of importing .SQL files into a MySQL database from

  • 0

I am building a way of importing .SQL files into a MySQL database from PHP. This is used for executing batches of queries. The issue I am having is error reporting.

$command = "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file";
exec($command, $output);

This is essentially how I am importing my .sql file into my database. The issue is that I have no way of knowing if any errors occurred within the PHP script executing this command. Successful imports are entirely indistinguishable from a failure.

I have tried:

  • Using PHP’s sql error reporting functions.
  • Adding the verbose argument to the command and examining the output. It simply returns the contents of the .sql file and that is all.
  • Setting errors to a user variable within the .sql file and querying it from the PHP script.

I hope I am not forced to write the errors into a temporary table. Is there a better way?

UPDATE:
If possible, it would be very preferable if I could determine WHAT errors occurred, not simply IF one occurred.

  • 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-11T16:13:20+00:00Added an answer on June 11, 2026 at 4:13 pm

    Try using shell_exec

    $output = shell_exec( "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file" );
    // parse $output here for errors
    

    From the manual:

    shell_exec — Execute command via shell and return the complete output as a string

    Note:

    This function is disabled when PHP is running in safe mode.

    EDIT: Full solution:

    what you need to do is grab STDERR and discard STDOUT. Do this by adding ‘2>&1 1> /dev/null’ to the end of your command.

    $output = shell_exec( "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file 2>&1 1> /dev/null" );
    $lines = explode( PHP_EOL, $output );
    
    $errors = array();
    
    foreach( $lines as $line )
    {
        if ( strtolower( substr( $line, 0, 5 ) ) == 'error' )
        {
            $errors[] = $line;
        }
    }
    
    if ( count( $errors ) )
    {
        echo PHP_EOL . 'Errors occurred during import.';
        echo implode( PHP_EOL, $errors );
    }
    else
    {
        echo 'No Errors' . PHP_EOL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app generates SSIS packages for importing data from Excel files. This is done
I'm building an SSIS package resposible for importing an Excel file sourced either from
Im building a dynamic sql update statement. This statement will update an unknown number
I'm building an editor that works with .CSV files. I have the application importing
I'm not sure if this is a bad way of building an application but
I'm building a web application with php and mysql. Web application will have 3-4
I am currently considering the best way of building a relatively complex page in
I've encountered a problem where part way through building a solution, VS 2010 becomes
Is there an easy way of dynamically building a filepath in .Net? At the
I am building a Firefox extension and have come a long way. One of

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.