tuchawat tuchawat - 16 days ago 10
MySQL Question

why if($row['field'] == value) work like SELECT WHERE field = value?

Today I'm drunked. I write wrong code like this.

$sql = "SELECT id, firstname, lastname, status FROM MyGuests";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
if($row['status'] == 'Active') {
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
}
}


and it work like SELECT WHERE $row['status'] == 'Active' .
It not make sense for me why is it work?

I think if statement just check the one column, and not relative to that all row column. It should not work.

Answer

The difference is:

  1. if($row['status'] == 'Active') { Here you bring all table records from the db to your app. So this have a cost in transport all the data.

  2. WHERE status= 'Active' Do the filter in the db, and only send the relevant data, so is more efficient.