select and display certain mysql data in php

Hello everyone,

I’m practicing PHP by giving myself random assignments to complete. I managed to select and query data from a mysql database table and echo it out into my php page.

My question is: How can I create some logic in my php mysql_query SELECT statement that would allow me to display records that only meet certain criteria. Such as: I have 4 columns (id, itemName, clothingType, quantity, price). I only want to display prices that are between $40 and $70 and exclude any other records.

Here is an example of my current code that displays everything:

[php]


<?php
$con=mysqli_connect(“localhost”,“root”,"",“test”);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
			$result = mysqli_query($con,"SELECT * FROM clothes");
			
			echo "<div>";

			while($row = mysqli_fetch_array($result))
			  {
			  echo "<ul>";
			  echo "<li>" . "Name: " . $row['itemName'] . "</li>";
			  echo "<li>" . "Type: " . $row['clothingType'] . "</li>";
			  echo "<li>" . "Quantity: " . $row['quantity'] . "</li>";
			  echo "<li>" . "$" . $row['price'] . "</li>";
			  echo "</ul>";
			  }
			echo "</div>";

			mysqli_close($con);
		?>
	</div>

[/php]

Any suggestions would be great!

Thanks

MySQL has a bunch of useful operands. One of which is the WHERE clause. In your example:

SELECT * FROM clothes WHERE price >= 40 AND price <= 70

Bear in mind that you will force MySQL to perform a full table lookup if you do not index your price field.

You’re the greatest! Thanks for your help. It worked perfectly!

Sponsor our Newsletter | Privacy Policy | Terms of Service