Solved

MYSQL Query doesn't work in PHP

Posted on 2009-07-08
3
282 Views
Last Modified: 2013-12-13
I want to use php file to import data from csv to mysql database.
The database can be connected successfully.
But the query of importing data is unsuccessful.
I print out the query in the page to see the code. It shows like this:

    LOAD DATA LOCAL INFILE 'C:\\xampp\\tmp\\phpCB5.tmp' INTO TABLE `thisweek` FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\r\n'

It appears the same as the code from mysql when I directly import .csv file into mysql database.
I don't know where the problem is.
Can anyone help?
<?php

session_start();

 

if ($_FILES["file"]["error"] > 0)

  {

  echo "Error: " . $_FILES["file"]["error"] . "<br />";

  }

else

  {

  echo "Upload: " . $_FILES["file"]["name"] . "<br />";

  echo "Type: " . $_FILES["file"]["type"] . "<br />";

  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

  echo "Stored in: " . $_FILES["file"]["tmp_name"] . "<br>";

  }

 

  

$usrName = "root";

$usrPwd = "";

$hostName = "localhost";

$database = "weekly_incidence";

  

$link = mysql_connect("$hostName", "$usrName", "$usrPwd") or die ("Error!" . mysql_error());

$res0 = mysql_query("USE $database", $link) or die("Use DB problem");

 

$sql_upload0 = "DELETE FROM `thisweek`";

print "<br>";

 

$sql = stripslashes("LOAD DATA LOCAL INFILE '") . addslashes($_FILES["file"]["tmp_name"]) . stripslashes("' INTO TABLE `thisweek` FIELDS TERMINATED BY ',' ENCLOSED BY '&quot' ESCAPED BY '") . "\\\\" . stripslashes("' LINES TERMINATED BY '") . "\\r\\n" . stripslashes("'");

 

print $sql;

print "<br>";

//mysql_query($sql_upload0) or die("die 0");

mysql_query($sql) or die("die 1");

 

 

?>

Open in new window

0
Comment
Question by:blackcatd_304
3 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24814127
Change line 33 into this:

mysql_query($sql) or die(mysql_error());

What is the error message?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24816713
You might want to use double quotes in that $sql statement.  Not sure, but single quotes around escape characters may give PHP some issues.
0
 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 50 total points
ID: 24819181
try:
<?php

session_start();

 

if ($_FILES["file"]["error"] > 0)

  {

  echo "Error: " . $_FILES["file"]["error"] . "<br />";

  }

else

  {

  echo "Upload: " . $_FILES["file"]["name"] . "<br />";

  echo "Type: " . $_FILES["file"]["type"] . "<br />";

  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

  echo "Stored in: " . $_FILES["file"]["tmp_name"] . "<br>";

  }

 

  

$usrName = "root";

$usrPwd = "";

$hostName = "localhost";

$database = "weekly_incidence";

  

$link = mysql_connect("$hostName", "$usrName", "$usrPwd") or die ("Error!" . mysql_error());

$res0 = mysql_query("USE $database", $link) or die("Use DB problem");

 

$sql_upload0 = "DELETE FROM `thisweek`";

print "<br>";

 

$sql = "LOAD DATA LOCAL INFILE '" . addslashes($_FILES["file"]["tmp_name"]) . "' INTO TABLE `thisweek` FIELDS TERMINATED BY ',' ENCLOSED BY '\'' ESCAPED BY '\\\\' LINES TERMINATED BY '\\r\\n'");

 

print $sql;

print "<br>";

//mysql_query($sql_upload0) or die("die 0");

mysql_query($sql) or die("die 1");

 

 

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Healthcheck 2 84
How can I run the php script or the page without visiting the page? 6 51
How can I upload multiple images from a HTML form? 2 18
html input type 3 21
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now