Solved

MYSQL Query doesn't work in PHP

Posted on 2009-07-08
3
281 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

12 Experts available now in Live!

Get 1:1 Help Now