-2

I want to update database table by array,currently iam using a code which take more time the code is below

  $data = array(
                'name' => $id
                 );

        while($data)
                {
              $pid=$data['name']; 
              mysql_query("UPDATE pm_registration SET status='1' WHERE id='$pid'")or die(mysql_error());
                }

is there any other solution ? Any assistance is very much appreciated.

sareef
  • 1
  • 2
  • See IN(). Typically, you would build the query inside the loop and then execute it, once, outside the loop. There is however a caveat to this approach, but maybe someone else can explain what that is – Strawberry May 18 '17 at 07:13

1 Answers1

0

First of all, I see you are using deprecated mysql extension. You are leaving yourself open to lots of security errors and SQL Injection. see How can I prevent SQL injection in PHP? for more detail.

I assume from your question you want to loop through an array and perform the same query you could achieve this by using Prepared statements and passing through the parameters.

e.g.

 $stmt = $pdo->prepare("UPDATE pm_registration SET status= :status WHERE id=:name");

    foreach ($data as $key => $value) {
        $stmt->execute(["status" => $value['status'], "name" => $value['name']]);
    }
Community
  • 1
  • 1
  • Thanks for trying to help me .you are right,using `foreach` instead of `while loop` is better and it take less exicution time. – sareef May 18 '17 at 09:57