Link to home
Create AccountLog in
Avatar of codemonkey2480
codemonkey2480

asked on

PHP - Iterating through the records from SELECT

I have a SELECT that returns a set of records for all my customers.
Ex:
Customer       Message
----------------------------
C1                  M1
C1                  M2
C1                  M3
C2                  M1
C2                  M2

I would like to get this in an array iterate through records for each Customer and echo it to the screen.

I am have done the DB connection and am able to get the records to a variable. How do I iterate through them and echo for each customer?
Avatar of Rik-Legger
Rik-Legger
Flag of undefined image

$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

Open in new window


http://php.net/manual/en/function.mysql-fetch-assoc.php
Avatar of k_romych
k_romych

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM customer_messages");

while($row = mysql_fetch_array($result))
  {
  echo $row['customer'] . " " . $row['message'];
  echo "<br />";
  }

mysql_close($con);
?>
Avatar of codemonkey2480

ASKER

Thanks for the quick reply. Will this do this?
 Output to Screen:

Customer C1
---------------
M1
M2
M3

Customer C2
---------------
M1
M2

Output messages for each customer?

ASKER CERTIFIED SOLUTION
Avatar of Rik-Legger
Rik-Legger
Flag of undefined image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
if you want to group by customer try  this one:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM customer_messages ORDER BY customers");
$customers=array();  messages=array();  $c_id=1 ;
while($row = mysql_fetch_array($result))
  {
   $this_customer=$row['customer'];  
   if (!in_array($this_customer,$customers)){
    $c_id+=$c_id;
      $customers[$c_id]=$this_customer;
      }
   $messages[$c_id][]=$row['message'];
   
   
  //echo $row['customer'] . " " . $row['message'];
  //echo "<br />";
  }
foreach($customers as $c_value){
 echo "Customer". $customers[$c_value]. "<br/>";
 foreach ($messages[$c_value] as $message){
      echo $message . "<br/>";
}
      
}

mysql_close($con);
?>
I am using Oracle. Please see attached code. I know the records are returned and outside the if they echo but any thing inside the IF statement is not echoed.
if($db->select($reports_sql))
	{	
		
		while($row = $db->fetch_assoc())
		{
			$sClient = '';
			 if ($row['CLIENTID'] != $sClient) 
			{
	       		echo $row['CLIENTID'] . '<br />----------<br />;
				echo $row['MESSAGE'] . '<br />----------<br />;
			}
			$sClient = $row["CLIENTID"];
			
		}
	 }

Open in new window

if($db->select($reports_sql))
	{	
		$sClient = '';
		while($row = $db->fetch_assoc())
		{
			
			 if ($row['CLIENTID'] != $sClient) 
			{
	       		echo $row['CLIENTID'] . '<br />----------<br />;
				echo $row['MESSAGE'] . '<br />----------<br />;
			}
			$sClient = $row["CLIENTID"];
			
		}
	 }

Open in new window

One final item: I have a WHERE clause in my SELECT and the like symbol % seems to be causing trouble.
How do I correct it?
select cm.id,cm.name,log.message,log.LOGSTAMP From admin_log log, client_master cm 
			  where log.clientid=cm.id --AND (log.message like '%Exception%' OR log.message like '%failed%') 
			  AND log.LOGSTAMP >= to_date(to_char(sysdate-1,'MM/DD/YYYY') || '12:00:00','MM/DD/YYYY HH24:MI:SS')

Open in new window

Perhaps you should make this into an other question because it is not really related to the initial question.
k_romych,
How do I do this for Oracle?
sorry codemonkey2480, I dont know much about Oracle
Just try adding "ORDER BY cm.name" at the end