Results 1 to 2 of 2

Thread: Using Foreach/Loop on a query

  1. #1
    Join Date
    Jul 2006
    Posts
    64
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Default Using Foreach/Loop on a query

    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?

    PHP Code:
    $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'"
    ); 

  2. #2
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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

    Code:
    <?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);
    }
    
    ?>

  3. The Following User Says Thank You to kuau For This Useful Post:

    acctman (09-02-2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •