Log in

View Full Version : Using Foreach/Loop on a query



acctman
09-01-2008, 07:23 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'");

kuau
09-02-2008, 06:31 AM
I don't know where all the php geniuses are today, but seeing as no one has tried to help you yet, here is my attempt. I have no clue about the decode part but I think this might process all rows in the table. I'm just learning php but hopefully this will help. e


<?php

$sql = " SELECT `m_id` FROM `rate_mem`";

$result = mysql_query($sql,$connection) or die("Couldn't execute $sql query. <br> mysql error: ".mysql_error());
$memberList = array();

while ($row = mysql_fetch_array($result))
{
$member = array();
$member['id'] = $row['m_id'];
$member['m_addtn'] = $row['m_addtn'];

$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'];


$sql = "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 = '".$member['id']."' ";

$result2 = @mysql_query($sql,$connection) or die("Couldn't execute $sql query. <br> mysql error: ".mysql_error());
$row2 = mysql_fetch_array($result2);

mysql_free_result($result2);
unset($row2);

array_push($memberList, $member);
}

?>