Geoff
Geoff

Reputation: 6629

Outputting data in php from sql

I would like to output grouped data from mysql in php

The sql database: enter image description here

I have an sql statement that groups the content according to a to_id of 4.

sql statement: select group_concat(messg) from my_table  where to_id = 4   
group by from_id;

I would like to view the messages in php such that i get:

1. get: 2messages  from_id 1(messages: TRUELOVE, hadi when)
2. get: 1 msg from_id 2(message: cutie)
3. get: 1 msg from_id 3(message: true love)

STATEMENTS TO CONNECT TO DATABASE:

<?php
$con = mysqli_connect("localhost", "root","","bmcs" );

if(mysqli_connect_errno()){

  echo "Failed to connect to database". mysqli_connect_error();
}

Upvotes: 0

Views: 65

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

with mysqli shoudl be this code

  <?php 
    $sql = "SELECT 
            group_concat(messg) as msg, from_id, time_sent
            from my_table 
            where to_id = 4   
            group by from_id"; 

        if ($result = $mysqli->query($sql)) { 
            while($obj = $result->fetch_object()){ 
                echo $obj->msg . '<br />'; 
                echo $obj->from_id . '<br />'; 
                echo $obj->time_sent. '<br />'; 


            } 
        } 
        $result->close(); 
        unset($obj); 
        unset($sql); 
        unset($query); 

?> 

Upvotes: 1

Related Questions