while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
superman
Forum Commoner
Posts: 29
Joined: Tue Jul 08, 2003 2:54 am

while loop

Post by superman »

this can be done when only one record in the table, if more than one record in the table, it show the error message.
Warning: Supplied argument is not a valid MySQL result resource in c:\phpweb/test5.php on line 6
how can i change this code to select the first row first, run the script, and then select the second row, run the scriptand so on.
then update the the different result in different rows.

+-------+------+------+
| User | Year | Days |
+-------+------+------+
| Ken | 2002 | 5 |
| Peter | 2003 | 9 |
+-------+------+------+

Code: Select all

<?
$query = "SELECT Year, Days FROM user"; 
$result = mysql_query($query); 
while($row = mysql_fetch_array($result)) 
{
  $CurrentYear = date("Y");
  $Diff = $CurrentYear - $Year;
  $Year = $row['Year'];
  $Days = $row['Days'];
    if ($Diff <= 2){
	$Year = $Year + 1;
	$result = mysql_query("UPDATE user SET Year=$Year");
  }

.
.
.
.
.
}
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I think that this line...

Code: Select all

$result = mysql_query("UPDATE user SET Year=$Year");
...breaks it. Setting $result to a this query, will make the $result in the while-loop above choke...
superman
Forum Commoner
Posts: 29
Joined: Tue Jul 08, 2003 2:54 am

Post by superman »

if i remove the $result, it can update the table, but with the same $Year in 2 rows
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Post by RFairey »

Just use a different variable for the sql query within the loop:

$result2 = mysql_query("UPDATE etc......
superman
Forum Commoner
Posts: 29
Joined: Tue Jul 08, 2003 2:54 am

Post by superman »

i've tried, but the same problem arised.

Code: Select all

<?
$Connect = mysql_connect("localhost","root","");
mysql_select_db("mw");
$query = "SELECT Entitlement, YearStarted FROM user"; 
$result = mysql_query($query); 
while($row = mysql_fetch_array($result))
{
	$YearStarted = $row['YearStarted'];
	$Entitlement = $row['Entitlement'];
	$CurrentYear = date("Y");
	$YearsWorked = $CurrentYear - $YearStarted;
	if ($YearsWorked <= 2) {
		if ($Entitlement >= 10){
		$CarryForward = 10;
		$Entitlement = $CarryForward + 18;
		$result1=mysql_query("UPDATE user SET Entitlement=$Entitlement") or die(mysql_error().":<br>".$query);
		}
		else {
		$Entitlement = $Entitlement + 18;
		$result1=mysql_query("UPDATE user SET Entitlement=$Entitlement") or die(mysql_error().":<br>".$query);
		}
		}
	else {
		if ($Entitlement >= 10){
		$CarryForward = 10;
		$Entitlement = $CarryForward + 19;
		$result1=mysql_query("UPDATE user SET Entitlement=$Entitlement") or die(mysql_error().":<br>".$query);
		}
		else {
		$Entitlement = $Entitlement + 19;
		$result1=mysql_query("UPDATE user SET Entitlement=$Entitlement") or die(mysql_error().":<br>".$query);
		}
	}
}	
?>
tylerdurden
Forum Commoner
Posts: 66
Joined: Mon Jul 28, 2003 11:52 am
Location: Austria

Post by tylerdurden »

Try using

Code: Select all

UPDATE user SET Entitlement='$Entitlement'
(with single quotes).
superman
Forum Commoner
Posts: 29
Joined: Tue Jul 08, 2003 2:54 am

Post by superman »

i want it to check each row and update each row, how?
i have a table:

+--------+------------+-----------+
| StaffID | YearStarted | Entitlement |
+--------+------------+-----------+
| Ken | 2003 | 7 |
| Peter | 2002 | 17 |
+--------+------------+-----------+


when i run the script, it suppose to show this:

+--------+------------+-----------+
| StaffID | YearStarted | Entitlement |
+--------+------------+-----------+
| Ken | 2003 | 25 |
| Peter | 2002 | 28 |
+--------+------------+-----------+


but it show this:????

+--------+------------+-----------+
| StaffID | YearStarted | Entitlement |
+--------+------------+-----------+
| Ken | 2003 | 28 |
| Peter | 2002 | 28 |
+--------+------------+-----------+
Post Reply