Solved

MYSQL Query doesn't work in PHP

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 …

773 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