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?
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?
<?php
$con = mysql_connect("localhost", "peter","a bc123");
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);
?>
$con = mysql_connect("localhost",
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);
?>
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?
Output to Screen:
Customer C1
---------------
M1
M2
M3
Customer C2
---------------
M1
M2
Output messages for each customer?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
if you want to group by customer try this one:
<?php
$con = mysql_connect("localhost", "peter","a bc123");
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['custo mer'];
if (!in_array($this_customer, $customers )){
$c_id+=$c_id;
$customers[$c_id]=$this_cu stomer;
}
$messages[$c_id][]=$row['m essage'];
//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);
?>
<?php
$con = mysql_connect("localhost",
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['custo
if (!in_array($this_customer,
$c_id+=$c_id;
$customers[$c_id]=$this_cu
}
$messages[$c_id][]=$row['m
//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);
?>
ASKER
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"];
}
}
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"];
}
}
ASKER
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?
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')
Perhaps you should make this into an other question because it is not really related to the initial question.
ASKER
k_romych,
How do I do this for Oracle?
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
Just try adding "ORDER BY cm.name" at the end
Open in new window
http://php.net/manual/en/function.mysql-fetch-assoc.php