0

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?

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.