I just created a page that could get the name of the students in 2 schools using their school name(This is just for my practice). But when i execute the following code i don't get the name of the top student in each school whose name is at top in the database, But i get the rest of the list.
<?php require 'connect.php'; if(isset($_GET['school'])) { $school = trim($_GET['school']); $people = $db->prepare("SELECT id, name FROM users WHERE school = ? "); $people->bind_param('s', $school); $people->execute(); $people->bind_result($id, $name); if(!$people->fetch()) { echo "Invalid School Name!!"; } else { while($people->fetch()) { echo 'ID. ',$id, ' ',$name,'<br>'; } } } ?> This problem only occurs when i use if and else statements to output error message "Invalid School Name!!". If i just use the while loop to fetch data, i get the full list. Please tell me the why i miss the first name and a solution for this. Thank you.
if (!$people->fetch())is correct (partially)