Select query in php by example |
Code for select query in php with mysql_num_rows function
<?php require 'connect.inc.php'; $query = "SELECT `food`, `calories` FROM `food` WHERE `healthy_unhealthy`='h' AND `calories`='700' ORDER BY `id` DESC"; if ($query_run = mysql_query($query)) { if (mysql_num_rows($query_run)==NULL) { echo ' No result found.'; }else{ while ($query_row = mysql_fetch_assoc($query_run)) { $food = $query_row['food']; $calories = $query_row['calories']; echo $food.' has '.$calories.' calories.<br>'; } } } else { echo mysql_error(); } ?>
Output (Screenshot1)
Screenshot1 |
Now if you are not comfortable to look this example and understand what it does just look my article select query in php. As well as look at general articles using mysql recommended to read my all articles of Index of php. Now what we had done here is we select a specific field names from this table called food where unhealthy equals to u and calories equal to 700. So, its quite being specific and in this case we only retrieve one value that's the ice cream value here with the id of 4. Now what we will want to do is we able take user input in order to return the specific values. So, let say that i want the user to be able to specify if they want allow list of unhealthy food or healthy food or may be if they want to specify maximum and minimum calories amount for now we just going to for unhealthy and healthy food option.
Code for select query in php user input
<?php require 'connect.inc.php'; ?> <form action="index.php" method="GET"> Choose a food type: <select name="uh"> <option value="u">unhealthy</option> <option value="h">healthy</option> </select><br><br> <input type="submit" value="Submit"> </form> <?php if (isset($_GET['uh'])&&!empty($_GET['uh'])) { $uh = strtolower($_GET['uh']); if($uh=='u'||$uh=='h'){ $query = "SELECT `food`, `calories` FROM `food` WHERE `healthy_unhealthy`='$uh' ORDER BY `id` DESC"; if ($query_run = mysql_query($query)) { if (mysql_num_rows($query_run)==NULL) { echo ' No result found.'; }else{ while ($query_row = mysql_fetch_assoc($query_run)) { $food = $query_row['food']; $calories = $query_row['calories']; echo $food.' has '.$calories.' calories.<br>'; } } } else { echo mysql_error(); } } } ?>
Output (Screenshot2)
Screenshot2 |
In Above Example We are taking User Input. Now see Code We have to define Connect.inc.php in require function at the top of the page. So, we will Finish Php Code after Require function
<?php
require 'connect.inc.php
?>
After that we will start html form to take users Input form action will be on same page index.php method is going to be Get. So, just we can see what's going on in the url bar at the top. Now what we are retrieving is something like inside index.php we are aim for something like index.php?uh=u for unhealthy and index.php?uh=h for healthy. So, we are using this get method for url to specify value. So, what we want to do is we need to create field and i am going to say i am going to create select field and this is going to be called uh and inside we are going to have option.
<form action="index.php" method="GET">
Choose a food type:
<select name="uh">
<option value="u">unhealthy</option>
<option value="h">healthy</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
So, we will having 2 option which are healthy and unhealthy. So, option value equals we just say u for unhealthy food and option value equals h for healthy food. Then Submit Button say input type equal to submit and value of that say submit. So, that will be input by the user So, Choose a food type: So, it will choose type healthy or unhealthy. Now we will create a query that allows users to retrieve the data according to the users choice. Its very useful in everyday applications. So, I am going to click on unhealthy and click submit You can see in url bar index.php?uh=u and here if we click on healthy and click on submit index.php?uh=h. So we are processing get data through url we can process in our query.
So, Now we will check this values are submitted. So, i am going to say if isset $_GET uh then proceed futher. And also the fields of the form is we will check whether it is not empty .
if (isset($_GET['uh'])&&!empty($_GET['uh'])) {
Now we can say uh equal $_GET['uh'] and also we do this strtolower. It will check whether this variable $uh is equal to h or u before perform query. So, we can grape value $uh. Above query you can check whether $uh variable is equal to u or h
$uh = strtolower($_GET['uh']);
if($uh=='u'||$uh=='h'){
Now in query we will check whether where healthy_unhealthy equals $uh which is user selected input will retrieve query according to the users's choice. So, If the user will click on healthy. The value of uh in form will be submitted h then in php script it will checks and grape data according to the user input by retrieving the query. You can see output Screenshot2