Hello all,
I'm working on a piece of code to upload a CSV file and then import it into MySQL. The upload is working great. I've used PHPMyAdmin to insert the same CSV file before and it works beautifully. So I figured I would take their PHP generated code (PHPMyAdmin) and use it for my PHP version of the insert, but I'm running into trouble.

Here is what I have for code so far:


<?php
include("config.inc.php");
$csv=$_GET['csv'];
$csv="csv/$csv";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($database) or die( "Unable to select database");
$sql = 'LOAD DATA INFILE \'/tmp/phpJ8mXgn\' INTO TABLE `SYSTEMS` FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\' (`Hostname`, `IP`, `OS`, `OSRev`, `Manufacturer`, `Model`, `CPUs`, `Memory`, `Application`, `Support_Contract`, `Serial`, `Primary_Admin`, `Server_Room`, `Cabinet`, `RUs`, `Console_Server`, `Shutdown_Order`, `Bindview`, `Date_Aquired`, `Date_In_Service`, `Volts`, `Amps`, `Power_Supplies`, `Notes`)';
mysql_query($sql);
?>

I would like to use the variable $csv in place of \'/tmp/phpJ8mXgn\', which was generated by PHPMyAdmin, but I'm having a hard time making it work successfully. I've tried several variations of this and haven't had any luck.

Could anyone lend a suggestion on how to do this properly?

Thanks. 🙂

-Sys

    <?php
    include("config.inc.php");
    $csv=$_GET['csv'];
    $csv="csv/$csv";
    mysql_connect($dbhost,$dbuser,$dbpass);
    mysql_select_db($database) or die( "Unable to select database");
    $sql = 'LOAD DATA INFILE \'' . $csv . '\' INTO TABLE `SYSTEMS` FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\' (`Hostname`, `IP`, `OS`, `OSRev`, `Manufacturer`, `Model`, `CPUs`, `Memory`, `Application`, `Support_Contract`, `Serial`, `Primary_Admin`, `Server_Room`, `Cabinet`, `RUs`, `Console_Server`, `Shutdown_Order`, `Bindview`, `Date_Aquired`, `Date_In_Service`, `Volts`, `Amps`, `Power_Supplies`, `Notes`)';
    mysql_query($sql);
    ?> 
    

    Is this what you want?

      (RL)Ian wrote:
      <?php
      include("config.inc.php");
      $csv=$_GET['csv'];
      $csv="csv/$csv";
      mysql_connect($dbhost,$dbuser,$dbpass);
      mysql_select_db($database) or die( "Unable to select database");
      $sql = 'LOAD DATA INFILE \'' . $csv . '\' INTO TABLE `SYSTEMS` FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\' (`Hostname`, `IP`, `OS`, `OSRev`, `Manufacturer`, `Model`, `CPUs`, `Memory`, `Application`, `Support_Contract`, `Serial`, `Primary_Admin`, `Server_Room`, `Cabinet`, `RUs`, `Console_Server`, `Shutdown_Order`, `Bindview`, `Date_Aquired`, `Date_In_Service`, `Volts`, `Amps`, `Power_Supplies`, `Notes`)';
      mysql_query($sql);
      ?> 
      

      Is this what you want?

      Hello (RL)Ian,
      That appears like what I'm after, but when I test it, it doesn't report an error, but it also doesn't seem to insert any data into MySQL. I'm not sure what I could be missing..

        sysera wrote:

        Hello (RL)Ian,
        That appears like what I'm after, but when I test it, it doesn't report an error, but it also doesn't seem to insert any data into MySQL. I'm not sure what I could be missing..

        Change:

        ENCLOSED BY

        to

        OPTIONALLY ENCLOSED BY

        ref: http://dev.mysql.com/doc/refman/5.0/en/load-data.html

          Write a Reply...