Fabio_Siqueira Posted February 10, 2016 Share Posted February 10, 2016 Hello guys!! I want to build a function that read and save csv file in MySql. The problem is that I can't pass the while array in function 'cause i want to change the variable $sql sometimes just to reduce my code in the future. Ex: function connect(){ @mysql_connect("localhost", "root", ""); mysql_select_db("school"); } function import($sql=null){ $filename = $_GET['file']; $openfile = fopen($filename, "r"); while(($row = fgetcsv($openfile,2048,";"))!==FALSE) { mysql_query($sql) or die(mysql_error()); } fclose($openfile); } $report = "Insert into table (DocumentoSD,Descricao,Cod_Client,Cliente) values('$row[0]','$row[1]','$row[2]','$row[3]')"; connect(); Import($report); Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 10, 2016 Share Posted February 10, 2016 You already have a thread for the exact same problem. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 10, 2016 Share Posted February 10, 2016 1 - you should immediately remove all of the "MySQL_*' code and begin to use either mysqlI_* functions or PDO functions to access your db. If you read the manual you will see that MySQL has been deprecated and you will soon discover that you appl is broken. 2 - Currently you are passing a query statement that has values in it that don't exist. If you had turned on php error checking you would have seen messages telling you this. 3 - If you need to modify the input as you read it, why use a function at all? Try this: (note the use of the proper tags to break out this code example: (do a pdo connect to the db and select your dbname) // open the input $filename = $_GET['file']; // you should validate this input before using it!!! $openfile = fopen($filename, "r"); // write your query and prepare it $q = "Insert into table (DocumentoSD,Descricao,Cod_Client,Cliente) values(:fld1,:fld2,:fld3,:fld4)"; $qst = $pdo->prepare($q); // loop thru the input and do the inserts while($row = fgetcsv($openfile,2048,";")) { // do any input data modifications here $fld1 = $row[0]; $fld2 = $row[1]; $fld3 = $row[2]; $fld4 = $row[3]; // now assign the modified values to an array $parms = array(':fld1'=>$fld1, ':fld2'=>$fld2, ':fld3'=>$fld3, ':fld4'=>$fld4); if (!$qst->execute($parms)) { (handle an error message on failure) exit(); // ? } } close($openfile); echo "All done loading file"; exit(); The only function I would have here is the one that does my connection and selection - something I created when I started using PDO. Quote Link to comment Share on other sites More sharing options...
Fabio_Siqueira Posted February 11, 2016 Author Share Posted February 11, 2016 Okay! Thank you ginerjm!! You help a lot!! Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 11, 2016 Share Posted February 11, 2016 HTH! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.