Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • I'm getting import CSV error while uploading files in php

    • 1
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 205
    Answer it

    Hi i'm developing the panel where admin can import the csv file directly to mysql database. But I'm getting the error. My code is

    <?php
    //ENTER THE RELEVANT INFO BELOW
    $mysqlDatabaseName ='bilmos';
    $mysqlUserName ='root';
    $mysqlPassword ='23nbhfi9';
    $mysqlHostName ='localhost';
    $mysqlImportFilename ='backup_26dec.sql';
    //DONT EDIT BELOW THIS LINE
    //Export the database and output the status to the page
    $command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;
    exec($command,$output=array(),$worked);
    switch($worked){
        case 0:
            echo 'Import file <b>' .$mysqlImportFilename .'</b> File is uploaded  to Mysql<b>' .$mysqlDatabaseName .'</b>';
            break;
        case 1:
            echo 'Unable to upload, Try again or check if same file exists in the folder or not:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Filename:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';
            break;
    }
    ?>
    

    Output as error:-

    Unable to upload, Try again or check if same file exists in the folder or not:
    MySQL Database Name:    bilmos
    MySQL User Name:    root
    MySQL Host Name:    localhost
    MySQL Import Filename:  backup_26dec.sql
    

    Please tell me where i'm doing wrong?

 1 Answer(s)

  • please change line number 10 & 11
    10 - $command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;
    11 - exec($command,$output=array(),$worked);
    TO
    10 - $command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename .' 2>&1';
    11 - exec($command,$output,$worked);

    Just before line number 12 ADD

    var_dump($output);

    This should show you the actual error.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: