Sajad Asadi Sajad Asadi - 7 months ago 5
PHP Question

two arrays and just show them separately

i have tho arrays and both of them show results when i remove other one but i need to show them on one function this is my code :

<?php
include('connect-db.php');
$result = mysqli_query($conn, "SELECT * FROM my_table ORDER BY Email");
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}

$data = array();
while ($row = mysqli_fetch_array($result))
{
$data[] = $row['Email'];
}

sort($data);
echo join($data, ',')

$bax = array();

while ($row = mysqli_fetch_array($result))
{
$bax[] = $row['Name'];
}

echo join($bax, ',')
?>


thanks in advance

Answer

You can do it in one while loop like below:-

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
include('connect-db.php');
$result = mysqli_query($conn, "SELECT * FROM my_table ORDER BY Email");
if($result === FALSE) { 
    die(mysqli_error($conn)); // don't mix mysql_* with mysqli_*
}

$data = array();
$bax = array();
while ($row = mysqli_fetch_assoc($result)) // mysqli_fetch_assoc will be better because mysqli_fetch_array is a combination of numeric+associative array while mysqli_fetch_assoc is just giving associative array
{
    $data[] = $row['Email'];
    $bax[] = $row['Name'];
}

sort($data);
echo join($data, ','); // ; missed
echo join($bax, ','); // ; missed
?>

Note:- I cannot say anything about your connection code so check yourself. Also read comment.