Alexander009 Posted December 21, 2009 Share Posted December 21, 2009 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 More sharing options...
Jibberish Posted December 21, 2009 Share Posted December 21, 2009 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). Link to comment https://forums.phpfreaks.com/topic/185865-problem-with-sql-querry-in-php-script/#findComment-981459 Share on other sites More sharing options...
Alexander009 Posted December 21, 2009 Author Share Posted December 21, 2009 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 Link to comment https://forums.phpfreaks.com/topic/185865-problem-with-sql-querry-in-php-script/#findComment-981477 Share on other sites More sharing options...
ChemicalBliss Posted December 21, 2009 Share Posted December 21, 2009 lol waited to use that emoticon . 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. Link to comment https://forums.phpfreaks.com/topic/185865-problem-with-sql-querry-in-php-script/#findComment-981497 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.