Web Hosting Talk







View Full Version : Using Foreach/Loop on a query


acctman
09-01-2008, 04:10 PM
Hi, the current code I wrote below looks up one user ID (m_id = 39) and processes it, how would I process all rows in table rate_mem?


$res = mysql_query("SELECT m_addtn FROM rate_mem WHERE m_id = '39'");
$results = mysql_fetch_array($res,MYSQL_BOTH);

$decode = unserialize(base64_decode($results['m_addtn']));

$m_orientation = $decode['3'];
$m_status = $decode['6'];
$m_turn_on = $decode['4'];
$m_turn_off = $decode['1'];

mysql_query ("UPDATE rate_members SET
m_orientation = '$m_orientation',
m_status = '$m_status',
m_turn_on = '$m_turn_on',
m_turn_off = '$m_turn_off'
WHERE m_id = '39'");

Exoware
09-01-2008, 04:22 PM
My first attempt would be to implement wildcards after SELECT and UPDATE.

acctman
09-01-2008, 05:17 PM
I think it'll require a little bit more then just a wildcard

Exoware
09-01-2008, 05:29 PM
In that case, divulge a little bit more as to what is actually going on. The sort of information that is being processed, etc.

Jatinder
09-02-2008, 12:52 AM
This should do the trick:


<?php

$rs = mysql_query("SELECT * FROM rate_mem");
if($rs) {
while($row = mysql_fetch_assoc($rs)) {

$decode = unserialize(base64_decode($row['m_addtn']));

$m_orientation = $decode['3'];
$m_status = $decode['6'];
$m_turn_on = $decode['4'];
$m_turn_off = $decode['1'];

mysql_query ("UPDATE rate_members SET
m_orientation = '$m_orientation',
m_status = '$m_status',
m_turn_on = '$m_turn_on',
m_turn_off = '$m_turn_off'
WHERE m_id = '".$row['m_id']."'");
}
}

?>

acctman
09-02-2008, 11:05 AM
This should do the trick:


<?php

$rs = mysql_query("SELECT * FROM rate_mem");
if($rs) {
while($row = mysql_fetch_assoc($rs)) {

$decode = unserialize(base64_decode($row['m_addtn']));

$m_orientation = $decode['3'];
$m_status = $decode['6'];
$m_turn_on = $decode['4'];
$m_turn_off = $decode['1'];

mysql_query ("UPDATE rate_members SET
m_orientation = '$m_orientation',
m_status = '$m_status',
m_turn_on = '$m_turn_on',
m_turn_off = '$m_turn_off'
WHERE m_id = '".$row['m_id']."'");
}
}

?>



thanks, everything worked