Jump to content

Help! How to pass the array value to the function


Fabio_Siqueira

Recommended Posts

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.