₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,330,428 members, 8,445,440 topics. Date: Wednesday, 15 July 2026 at 05:17 AM

Toggle theme

PHP: Updating Record Within A While Loop - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingPHP: Updating Record Within A While Loop (1133 Views)

1 Reply

PHP: Updating Record Within A While Loop by uvalued(op): 3:37pm On May 02, 2016
Hello guys, I need your support with respect to the code below..

The logic is this, Get the id,fin from a table called entry then add 1 to fin and update the fin column with fin=fin+1. It does for the first row but stops. I want all the fin column updated with fin=fin+1 where id = 1,2,3,4 etc



<?php

include 'connection.php';



$sql = "SELECT count(*) FROM entry";
$result = $dbh->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();

if ($number_of_rows > 0 )

{

$stmt = $dbh->prepare("SELECT id,fin FROM entry" );
$stmt->execute();

while($row = $stmt->fetch(PDO::FETCH_ASSOC) )
{
$id = $row['id'];
$finx = $row['fin'];


$stmt = $dbh->prepare("UPDATE entry SET
fin=:fin
WHERE id=:id" );

$stmt->bindparam(':fin', $session);
$stmt->bindparam(':id', $id);
$stmt->execute();
}
}



$dbh = null;


?>
Re: PHP: Updating Record Within A While Loop by spikesC(m):
1. There's no need to perform the first query. Any SELECT query returns the num_of_rows
2. Don't run queries in loops. Build the query string in the loop and run the query after looping
3. I don't know where the variable $session comes from

Finally, your solution.
A single query would actually solve your problem.

UPDATE entry SET fin = fin+1

Yep, Just that.
Re: PHP: Updating Record Within A While Loop by uvalued(op): 8:50pm On May 02, 2016
spikesC:
1. There's no perform the first query. Any SELECT query returns the num_of_rows
2. Don't run queries in loops. Build the build string in the loop and run the query after looping
3.I don't know where the variable $session comes from

Finally, your solution.
A single query would actually solve your problem.

UPDATE entry SET fin = fin+1

Yep, Just that.
thanks a alot... simply doing UPDATE entry SET fin = fin+1 in phpmyadmin did the trick... thanks
1 Reply

Javascript - While LoopHelp With Python Program (simple While Loop)Programming Challenge: Convert String to Json with a Loop234

A Simpler Website Testing Tool Is Launched By Google Targeting Small BusinessesAny Web Dev's In OTA?Health Tech Meetup