-2

I want to display a list of names and separate them with a comma and the last name should be separated with an "and" using php. Please I need help.

Here is my code:

    <?php 
                  $bdaypersoname="";
                  do {
                     $bdaypersoname .= $row_Recordset2['Name']." , ";

                      } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); 
                      $bdaypersoname= rtrim($bdaypersoname, " , ");
                      echo $bdaypersoname;
                    ?>
    </p>
Red fx
  • 1,071
  • 2
  • 12
  • 26
  • As in: "Please stop using PHP's archaic, insecure, *and* deprecated mysql_ API" ? – Strawberry Oct 30 '17 at 09:08
  • can you post sql you used ? – User123456 Oct 30 '17 at 09:21
  • Here is the sql code.................................... mysql_select_db($database_twconn, $twconn); $query_Recordset2 = "SELECT Name, DOB FROM tb1 WHERE month(DOB)=month(curdate())"; $Recordset2 = mysql_query($query_Recordset2, $twconn) or die(mysql_error()); $row_Recordset2 = mysql_fetch_assoc($Recordset2); $totalRows_Recordset2 = mysql_num_rows($Recordset2); – Pearl Silver Oct 31 '17 at 08:41

1 Answers1

0

Try like this, with some demo names

$array = ['Alex','Bob','Collins','Danny'];
echo join(' and ', array_filter(array_merge(array(join(', ', array_slice($array, 0, -1))), array_slice($array, -1)), 'strlen'));
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103