0

I'm following the first answer given here (Importing CSV data using PHP/MySQL) but I'm probably messing up some basic thing.

Here how my code looks now:

<?php

$accounts =  mysql_connect('localhost','root','mypass')
or die (mysql_error());
mysql_select_db("dbname",$accounts);

$fileName='./myfile.csv';

$query = <<<eof
    LOAD DATA INFILE '$myfile.csv'
     INTO TABLE tbname
     FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
     LINES TERMINATED BY '\n'
    (ID,Entity,HOOK,Period,Status,Trade,etc)
eof;

$dbname->query($query);
?>

Im getting and error in my last line as it follows:

"Undefined variable: dbname in C:\xampp\htdocs\mycode\loadcsv.php on line 17"

which prob mean that I didn't properly load that &dbname is as a db? I did managed to retrieve data from the same db using the same connection.

Any help in what the line

$dbname->query($query);

does is welcomed!

tks in advance!

Community
  • 1
  • 1
  • If you work with `mysql_connect` and `mysql_select_db`, you should also use `mysql_query` for your query. – PKeidel Dec 06 '13 at 15:26
  • @PKeidel. can you give a bit more insight? I tried:`$dbname->mysql_query($query);` and `$dbname->mysql_query($query, $account);` and didn't work! Any tips are welcomed! –  Dec 06 '13 at 16:35
  • I posted the correct code as an answer below ;) – PKeidel Dec 07 '13 at 12:04

1 Answers1

0

As I mentioned in my comment before, you're mixing procedural and objective programming. Functions like mysql_connect, mysql_select_db and mysql_query are procedural. So you have to go that way when you startet your project with this functions.

Your code should look someting like this:

<?php
$accounts =  mysql_connect('localhost','root','mypass') or die (mysql_error());
mysql_select_db("dbname",$accounts);

$fileName='./myfile.csv';

$query = <<<eof
    LOAD DATA INFILE '$myfile.csv'
     INTO TABLE tbname
     FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
     LINES TERMINATED BY '\n'
    (ID,Entity,HOOK,Period,Status,Trade,etc)
eof;

mysql_query($query); // <= HERE is the change
?>

You can't use $dbname because it is a variable that doesn't exists.

PKeidel
  • 2,559
  • 1
  • 21
  • 29
  • @PKeidel.Tks again but I'm having other issue bcs the change above did not work. I also tried to move the entire code to mysqli object oriented, but still stuck :-/(http://stackoverflow.com/questions/20436030/importing-csv-data-using-php-mysql-mysqli-syntax). –  Dec 07 '13 at 14:23
  • Not for this code. It runs fine its just that it does not upload anything in my tables. –  Dec 07 '13 at 19:19
  • Try `echo $query` and run the shown SQL statement via phpmyadmin or the console to check its functionality. Also add an `error_reporting(E_ALL)` at the beginning of your file. – PKeidel Dec 08 '13 at 11:05