I have been using the following query to upload my data into mysql database:
$sql = array();
foreach( $data as $row ) {
$sql[] = '("'.mysql_real_escape_string($row['text']).'", '.$row['category_id'].')';
}
mysql_query('INSERT INTO table (text, category) VALUES '.implode(',', $sql));
Since a bit I have started using PDO and my query looks like this:
$query="INSERT INTO mytable (name, use) VALUES(:sname, :usee)";
$res = $db_conn->prepare($query);
$res->bindValue(':sname',$value);
$res->bindValue(':usee',$_SESSION['usee']);
$res->execute();
Now the above code block is fine, but now when I am going through my CSV upload thing, I again looking backward and using the first code. Want to use the same PDO now for CSV upload also.
Is there a trick to upload multiple values in database using PDO at once?
Yes, you can use some loop in which you will execute, something like this:
$query = $db->prepare(
'INSERT INTO mytable (name, use) VALUES(:sname, :usee)'
);
foreach($mainArrayOfveluus AS $arrayOfValue){
$query->execute(array(
':sname' => $arrayOfValue['sname'],
':usee' =>$arrayOfValue['usee']
));
}
$query->commit();