Jump to content

problem with sql querry in php script


Alexander009

Recommended Posts

Hello guys,

 

What I want to do is I want to read out a csv file and then but te conent ot the file in a msql datbase .

So far it works but the problem I haven that if I want to let te script to the typing for me so that I dont have to fill in the Values of the MySQL query myself It doesn't work

 

This is my code so far :

 

<?php

$dbhost = 'localhost';

$dbuser = 'y';

$dbpass = 'x';

$dbname = 'test';

 

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die;

mysql_select_db($dbname);

 

 

$file = fopen("test.csv", "r") or exit("Unable to open file!");

//Output a line of the file until the end is reached

while(!feof($file))

  {

 

  echo fgets($file). "<br />";

  }

fclose($file);

 

mysql_query("INSERT INTO test

(Naam,Geboortedatum, Leeftijd,woonplaats, Geslacht) VALUES('$file','$file', '$file','$file','$file' ) ")

or die(mysql_error());

 

 

if (!mysql_query($sql,$conn))

  {

  die('Error: ' . mysql_error());

 

echo "Gegevens toegevoegd";

      }

mysql_close($conn)

?>

Link to comment
https://forums.phpfreaks.com/topic/185865-problem-with-sql-querry-in-php-script/
Share on other sites

You could use the fgetcsv() function istead as that returns the csv line as an array.

 

<?php

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

mysql_query("INSERT INTO test
(Naam,Geboortedatum, Leeftijd,woonplaats, Geslacht) VALUES('$data[0]','$data[1]', '$data[2]','$data[3]','$data[4]' ) ")
or die(mysql_error());

}
?>

 

 

Somthing along those lines (If you want to put each line into a new row of the table).

 

what do you mean exactly I am new to php so ,

 

i have now this and get errors allover the place :s

 

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die;

mysql_select_db($dbname);

 

 

$file = fopen("test.csv", "r") or exit("Unable to open file!");

//Output a line of the file until the end is reached

while(!feof($file))

  {

 

  echo fgets($file). "<br />";

  }

fclose($file);

 

 

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

 

mysql_query("INSERT INTO test

(Naam,Geboortedatum, Leeftijd,woonplaats, Geslacht) VALUES('$data[0]','$data[1]', '$data[2]','$data[3]','$data[4]' ) ")

or die(mysql_error());

 

}

 

 

 

if (!mysql_query($sql,$conn))

  {

  die('Error: ' . mysql_error());

 

echo "Gegevens toegevoegd";

      }

mysql_close($conn)

?>

 

Warning: fgetcsv() expects parameter 1 to be resource, null given in C:\Program Files\xampp\htdocs\slide\index.php on line 21

:rtfm: lol waited to use that emoticon :P.

 

The manual says (And the error), that paramter 1 needs to be of type: resource (this would be returned by a successfull fopen call).

 

$handle doesnt exist anywhere in the code so obviously its wrong, where is your resource? what variable are you using to save the resource to (from fopen()).

 

Hope this helps,

-CB-

 

PS:

http://uk3.php.net/manual/en/function.fgetcsv.php

The manual is your best friend.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.