The while/if/else statement I set up works except for the fact that the no results message shows up regardless of whether resuts are returned or not. I've tried setting the "break" statement so it allows the loop to exit appropriately, but I can't get it in the right spot.
<html>
<head>
</head>
<body>
<p>Search the Customer Database</p>
<form action="Index.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// If there is a search variable try to search database
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql = "SELECT * FROM Customers WHERE Client LIKE '%$searchq%'";
if ($result = mysqli_query($conn, $sql)) {
/* fetch associative array */
while (true)
if ($row = mysqli_fetch_row($result)) {
printf ("%s (%s)<br/>", $row[0], $row[1]); }
else {printf ("There were no Results");
break;
/* free result set */
mysqli_free_result($result);
}
}
/* close connection */
mysqli_close($conn);
}
?>
</body>
</html>