Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MYSQL Query doesn't work in PHP

Posted on 2009-07-08
3
Medium Priority
?
291 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 111

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 150 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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