1

I have to get all the phone numbers of students from database and store in string variable with a comma in between them. I have tried the following but failed.

This is my code below :

        $toNumbersCsv="";
        $this->db->select("std_cellNo");
        $this->db->from("student");

        $queryforPhone = $this->db->get();
        //Attempt 1
        // while ($row = mysql_fetch_assoc($queryforPhone)) {
            // $toNumbersCsv .= $row['std_cellNo'].',';
        // }
        //Attempt 2
        foreach($queryforPhone as $qfp){
            $toNumbersCsv .= $qfp.',';
        }
Pradeep
  • 9,667
  • 13
  • 27
  • 34

2 Answers2

0
foreach($queryforPhone->result() as $qfp){
        $toNumbersCsv .= $qfp->std_cellNo.',';
}
0

Hope this will help you :

use result() to get the record from the query also

$toNumbersCsv = "";
$this->db->select("std_cellNo");
$this->db->from("student");
$results = $this->db->get()->result();
foreach ($results as  $row) 
{
   $toNumbersCsv .= $row->std_cellNo.',';
}
echo rtrim($toNumbersCsv, ',');

for more : https://www.codeigniter.com/user_guide/database/results.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34