-1

I have this code:

$sql = "SELECT id FROM videos";
$stmp = $db->prepare($sql);
$stmp->execute();
while ($row = $stmp->fetchAll(PDO::FETCH_ASSOC)) {

    $vkey = md5($row['id'] . "video");

    $sql = "UPDATE videos SET vkey = :vkey WHERE id = :id";
    $stmp = $db->prepare($sql);
    $stmp->execute(array(
        ":vkey" => $vkey,
        ":id"   => $row['id']
    ));

}

Why is execute only for the first id from the first select and not for all it's in the loop?

6
  • There are two execute() calls in your code, one is in the loop. I guess I don't understand your question? Commented Sep 26, 2018 at 18:20
  • first select get all id's from table videos and in loop i want to execute query for each id Commented Sep 26, 2018 at 18:22
  • It’s better to fetch all into array and then loop through it stackoverflow.com/a/15386703/3254405 Commented Sep 26, 2018 at 18:26
  • You overwrite $stmp. You also should prepare outside of the loop. Commented Sep 26, 2018 at 18:32
  • 2
    Why not use MySQL's MD5() for a pure SQL call: UPDATE videos SET vkey = MD5(vkey);? Commented Sep 26, 2018 at 18:32

2 Answers 2

3

You could completely avoid all of that code by just doing this:

$db->query("UPDATE videos SET vkey = MD5(CONCAT(vkey, 'video'))");

(Or you could do this query in your backend like PHPMyAdmin UPDATE videos SET vkey = MD5(CONCAT(vkey, 'video')))


However, if you for some reason want to loop through your database, you could do this:

$sql = "SELECT id FROM videos";

//no reason to use prepare() because you aren't passing variables.
$stmp = $db->query($sql);
$stmp->execute();
$results = $stmp->fetchAll(PDO::FETCH_ASSOC);

//prepare UPDATE query outside of loop, this way you don't send 2 requests to your database for every row
$stmp = $db->prepare("UPDATE videos SET vkey = :vkey WHERE id = :id");

foreach($results as $result) {
    $vkey = md5($result['id']."video");
    $stmp->execute(
        array(
            ":vkey" => $vkey, 
            ":id" => $result['id']
        )
    );
}

Also, it's usually a good idea to check the return values inside the loop to make sure there were no errors, you could probably do this by using something like $stmp->rowCount() to check if there were any rows effected.

Sign up to request clarification or add additional context in comments.

4 Comments

Is not working. Is updated only for the first id from select.
Always a good idea to check return values to ensure no errors occurred as well.
@user1964450 Glad I could help!
@miken32 I'll add that to my answer, however I'm not gonna update the code because I'm not sure how OP wants to handle errors.
0

You need a

$stmt->closeCursor();

Otherwise the statement is stuck in the first run. You might want to check with a try...catch, echoing the error-message.

1 Comment

This answer should be deleted, because it's plainly wrong.

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.