SELECT / LIST Records From Database Table: PHP & MySQL

Advertisement:

Video tutorial demonstrates selecting / listing / fetching of data / records from the database table and displaying it on the browser.

First look at these short videos:
Connect to the database
Simple / basic insertion operation
Insertion of Records into Database using Forms: PHP & MySQL (important)

SELECTING and Displaying Data
index.php

1
2
3
4
5
6
7
8
9
< ?php
  include_once('db.php');
 
 $res = mysql_query("SELECT * FROM apple");
 
  while( $row = mysql_fetch_array($res) )
	  echo "$row[id]. $row[name] <br />";
 
?>

Video Tutorial: SELECT / LIST Records From Database Table: PHP & MySQL


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=OAT0iTXlqCk [Watch the Video In Full Screen.]



Full code
index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
< ?php
	include_once('db.php');
 
	if(isset($_POST['name']))
	{
	  $name = $_POST['name'];
 
	  if(mysql_query("INSERT INTO apple VALUES('','$name')"))
		echo "Successful Insertion!";
	  else
		echo "Please try again";
	}
 
 
	$res = mysql_query("SELECT * FROM apple");
 
 
?>
 
<form action="." method="POST">
Name: <input type="text" name="name"/><br />
<input type="submit" value=" Enter "/>
</form>
 
<h1>List of companies ..</h1>
< ?php
	while( $row = mysql_fetch_array($res) )
	  echo "$row[id]. $row[name] <br />";
?>

while loop executes until there is data inside the table, once there is no more data to fetch, loop terminates.

MySQL Query

1
mysql> SELECT * FROM apple;

This mysql statement fetches data from all the fields of the table.
We could specify fields and get only list of those values.

Ex:

1
mysql> SELECT id FROM apple;

Using this we can get list of id’s only.

After fetching data from table apple

List of companies ..

1. Google
2. Apple
3. Microsoft
4. Oracle
5. Technotip

Advertisements

2 thoughts on “SELECT / LIST Records From Database Table: PHP & MySQL”

  1. Hello Satish,
    i have a question. I see your videos and i try to build a simply search engine like in PhpAdmin. I want enter a word inside form and i want find that word
    I have a database called forms1, datatable called demo, 3 field called ID, Autore, Titolo.
    I’m using this code for display all values inside database:
    http://pastebin.com/dnPK47BQ
    But the problem is that i want FIND a specific word..If i search Satish like word i want see in result page only fields containing Satish words not all database. How code can i use?

  2. @elixir1349, Create a html form first, with a input box to put your search term.
    Next in it’s action field, put the name of your php file.

    Get the search term in your php file using POST or GET method(preferably GET method to this kind of applications). Assign the user entered search term to a php variable $search.

    Now modify your MySQL query:
    $sql = “SELECT * FROM demo WHERE Autore = ‘$search'”;

    $res = mysql_query($sql);

    Am assuming that the search term to be compared if present inside the field Autore.

    Hope this helps..

Leave a Reply

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