Google Ads

Retrieving Specific Data in MySQL/PHP PHP Script

Retrieving Specific Data in MySQL/PHP PHP Script

This tutorial is on how to retrieve specific data from a database through PHP and MySQL/MySQLi.

In my latest tutorials I have shown how to retrieve all rows from a database table, how to insert data, and how to validate data. Now I will show you how to retrieve specific data through slightly more advanced queries.

First you will obviously need a database with table. My database is named ‘fln’ and table is named ‘test’. It has two columns;
id, 5 length, primary key, auto increment, integer
score, 255 length, varchar

You can download the entire .sql file to import from the bottom of this tutorial article page. It is already populated with data so I will not run over that set, if you need to insert data, refer to one of my previous tutorials which can be found on my profile’s tacking page.

First we must make the PHP connection, like so…


  1. <?php
  2.       $con = mysqli_connect('localhost', 'root', '', 'fln'); //server, username, password, database name
  3. ?>



What Data?
Next we must decide what data we want to have returned to us. Since my table only really consists of scores, I am first going to retrieve all scores between 100 and 1000.

Query Method:
We could do this through a MySQLi query, like so…


  1. $q = mysqli_query($con, "SELECT * FROM `test` WHERE `score`>'99' AND `score`<'1001'");
  2. while ($row = mysqli_fetch_array($q)) {
  3.       echo 'Found query result; ' . $row['score'] . '<br/>';
  4. }



PHP Method:
Or, we could retrieve all the rows and filter them through PHP, like so…


  1. $q = mysqli_query($con, "SELECT * FROM `test`");
  2. $scores = array();
  3. while ($row = mysqli_fetch_array($q)) {
  4.       if ($row['score'] > 99 && $row['score'] < 1001)
  5.             array_push($scores, $row['score']);
  6. }
  7. foreach ($scores as $key => $value) {
  8.       echo $value . '<br/>';
  9. }



So now we have an array of the correct score results. – We also output each one through a simple foreach loop.





Related Post

CodeIgniter Ajax CRUD using jQuery PHP Script CodeIgniter Ajax CRUD using jQuery PHP Script I have prepared a database in this sourcecode, so follow the steps below to import it. 1. Open php...
Bear Auto Clicker PHP Script Download Bear Auto ClickerBear Auto ClickerAuto Clicker tool allows you to generate traffic on site by setting rules of clicking on links or buttons. User sets...
B2Evolution v4.1.6 Blog PHP Script B2Evolution v4.1.6 Blog PHP Script Create a wonderful Blog website by using this blog php script. b2evolution is an advanced weblog tool, i-e so...
Carbiz v1.2 – Buy Sell Car Marketplace PHP S... Easy 2 step installation Responsive HD design with Bootstrap and Codeigniter PHP framework 3 types of banner(Parallax slider, Google map and layer sli...
Restaurant Made Easy PHP Script Download Restaurant Made Easy Restaurant Made Easy Restaurant made easy is a restaurant food ordering management. that will help you boost your restaurant sale...

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *