-1

I have two tables in my database: appointments and users. They have the same named column: id.

When I made a mysqli_fetch_array for my query, how can I get the id from the appointments, not from the users? In default, the while loop get the 'users'->'id'.

$query_pagination = "SELECT ap.*, us.* 
                     FROM appointments AS ap, users AS us 
                     WHERE us.id = ap.user_id AND 
                           us.nev LIKE '%".$search."%'"


while($row = mysqli_fetch_array($result))
{
    echo $row["id"]; //i want to echo here the id from appointments
}
?>
SuperStar518
  • 2,814
  • 2
  • 20
  • 35
  • Have you considered using INNER JOIN and giving the id columns aliases – James Wright Oct 23 '19 at 15:47
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Oct 24 '19 at 19:02

1 Answers1

-1

You could rename the id from appointments by using AS like so:

$query_pagination = "SELECT ap.id AS `appointments_id`, us.* FROM appointments AS ap, users AS us WHERE us.id = ap.user_id AND us.nev LIKE '%".$search."%'"

while($row = mysqli_fetch_array($result))
{
    echo $row["appointments_id"]; //i want to echo here the id from appointments
}
YTZ
  • 876
  • 11
  • 26