SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP PDO

Collecting more than one record from table

WE can collect more than one record from a table by using sql query. For example list of all states of USA from the country list. Our table have two columns one is country and other is associated states.

We have already connected to database by using PDO. After successful connection to MySQL database we can use this script to display records from the table.

$sql="select state from table_name WHERE state='USA'";

echo "<table>";
foreach ($dbo->query($sql) as $row) {
echo "<tr ><td>$row[state]</td></tr>";
}
echo "</table>";

Let us add some style command to this code like this.

$i=1;
echo "<table class='t1'>";
foreach ($dbo->query($sql) as $row) {$m=$i%2;
echo "<tr class='r$m'><td>$row[state]</td></tr>";
$i=$i+1;}
echo "</table>";

Displaying Multiple columns of a table

We can display more than one column of the database table. Here we will keep column name in table header and keep the record data in normal table cell.

require "config.php";// database connection
$count="select * from pdo_admin where name='$name'";
echo "<table>";
echo "<tr><th>id</th><th>userid</th><th>password</th><th>name</th><th>status</th></tr>";
foreach ($dbo->query($count) as $row) {
echo "<tr ><td>$row[id]</td><td>$row[userid]</td><td>$row[password]</td><td>$row[name]</td><td>$row[status]</td></tr>";
}
echo "</table>";

Output with limited records are displayed here.

iduseridpasswordnamestatus
1admin1test1AdminT
2admin2test2AdminF
3admin3test3AdminT
6admin4test4AdminT
7admin5test5AdminA
8adm#in6test6AdminA
9admin7test7AdminA
10admi'n8test8AdminA
12admin9test9AdminA
13Admin10test10AdminA

We have displayed limited number of records here , but there may be hundred or more records in a table. Displaying all in a single page will not give better user experience. We can break the total records and display limited records and then give user choice to browse to different section of the records.

This is called paging of records.

Paging of records

Be the first to post comment on this article :


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer