Solved

MYSQL Query doesn't work in PHP

Posted on 2009-07-08
3
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
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…
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…

695 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