Jump to content

[SOLVED] Looping a query


Mutley

Recommended Posts

I have a cron that updates a field to "yes" where another field says "1", example:

 

user_id | validate | approve

1        | 1          | yes

2        | 0          |

3        | 0          |

4        | 1          | yes

 

At the moment I run it every hour and it updates just the first one,

 

so

 

UPDATE table SET `approve`= 'yes' WHERE validate = '1';

 

How do I do it to ALL of them with validate=1?

Link to comment
https://forums.phpfreaks.com/topic/50274-solved-looping-a-query/
Share on other sites

Hmm, maybe because I'm using SELECTs then, here's my code:

 

		$result = mysql_query("SELECT user_id, job_id FROM jobs WHERE done = '0'");
	while($row = mysql_fetch_array( $result ))
    	{
	$user_id = $row['user_id'];
	$job = $row['job_id'];
	}

	if($job == '1') {
	$wages = '5';
	} else {
	$wages = '0';
	}

	$result = mysql_query("SELECT money FROM users WHERE user_id = '$user_id'");
	while($row = mysql_fetch_array( $result ))
    	{
	$money = $row['money'];
	}

	$earnings = $money + $wages;


	$sql  = "UPDATE users SET money = '$earnings' WHERE user_id = '$user_id'";
	mysql_query($sql);

	$sql  = "UPDATE jobs SET done = '1' WHERE job_id = '1' AND user_id = '$user_id'";
	mysql_query($sql);

<?php
$result = mysql_query("SELECT user_id, job_id FROM jobs WHERE done = '0'");
	while($row = mysql_fetch_array( $result ))
    	{
	$user_id = $row['user_id']; //<--So this will only be the last result
	$job = $row['job_id'];
	} ///<--Ermm shouldn't this be \/

	if($job == '1') {
	$wages = '5';
	} else {
	$wages = '0';
	}

	$result = mysql_query("SELECT money FROM users WHERE user_id = '$user_id'");
	while($row = mysql_fetch_array( $result ))
    	{
	$money = $row['money'];
	}

	$earnings = $money + $wages;


	$sql  = "UPDATE users SET money = '$earnings' WHERE user_id = '$user_id'";
	mysql_query($sql);

	$sql  = "UPDATE jobs SET done = '1' WHERE job_id = '1' AND user_id = '$user_id'";
	mysql_query($sql);

//HERE
?>

try

 

<?php
$result = mysql_query("SELECT user_id, job_id FROM jobs WHERE done = '0'");
while($row = mysql_fetch_array( $result ))
{
	$user_id = $row['user_id'];
	$job = $row['job_id'];

	if($job == '1')
	{
		$wages = '5';
	} else {
		$wages = '0';
	}

	$result = mysql_query("SELECT money FROM users WHERE user_id = '$user_id'");
	while($row = mysql_fetch_array( $result ))
	{
		$money = $row['money'];

		$earnings = $money + $wages;

		$sql  = "UPDATE users SET money = '$earnings' WHERE user_id = '$user_id'";
		mysql_query($sql);

		$sql  = "UPDATE jobs SET done = '1' WHERE job_id = '1' AND user_id = '$user_id'";
		mysql_query($sql);
	}
}
?>

lol my bad


<?php
$result1 = mysql_query("SELECT user_id, job_id FROM jobs WHERE done = '0'");
while($row1 = mysql_fetch_array( $result1 ))
{
	$user_id = $row1['user_id'];
	$job = $row1['job_id'];

	if($job == '1')
	{
		$wages = '5';
	} else {
		$wages = '0';
	}

	$result2 = mysql_query("SELECT money FROM users WHERE user_id = '$user_id'");
	while($row2 = mysql_fetch_array( $result2 ))
	{
		$money = $row2['money'];

		$earnings = $money + $wages;

		$sql  = "UPDATE users SET money = '$earnings' WHERE user_id = '$user_id'";
		mysql_query($sql);

		$sql  = "UPDATE jobs SET done = '1' WHERE job_id = '1' AND user_id = '$user_id'";
		mysql_query($sql);
	}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.