Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,209,469 members, 8,006,189 topics. Date: Monday, 18 November 2024 at 05:58 PM

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

Nairaland Forum / Science/Technology / Programming / PHP: Updating Record Within A While Loop (1047 Views)

Javascript - While Loop / Help With Python Program (simple While Loop) / Programming Challenge: Convert String to Json with a Loop (2) (3) (4)

(1) (Reply)

PHP: Updating Record Within A While Loop by uvalued(m): 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): 6:08pm On May 02, 2016
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.

2 Likes

Re: PHP: Updating Record Within A While Loop by uvalued(m): 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)

Please Advice Me!!! / Embrace The Cloud And Deliver Business Value With Amazon Web Services / Urgently Needed - An Android App Developer

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.