stkmedia stkmedia - 5 months ago 7
SQL Question

select values from different columns mysql, how do I tell which is which?

$sql = "SELECT email FROM users WHERE username='$user1' OR username='$user2' LIMIT 2";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$email1 = $row[0];
$email2 = $row[1];


Just trying to select email from 2 columns and identity which email belongs to which username. Email1=user1 and email2=user2, is what I seek.

Answer
$sql = "SELECT username,email FROM users WHERE username='$user1' OR username='$user2' LIMIT 2";
$query = mysqli_query($con, $sql);
while($row = mysqli_fetch_row($query))
{
    $email[ $row[0]] = $row[1];

}
mysqli_free_result($row);

Hope it will help..

expected output

$email['user1']= email1
$email['user2']= email2