Worth2Read
Home

Forum

PHP Tutorials
Blogs
Children
Article Directory

Contact Us

Disclaimer
Reliable $1 Web Hosting by 3iX
 

If you have any doubts with your PHP code please discuss it in the PHP Forum

Display The Most Recently Added Results From An SQL Table

Relational database theory states that row order in a database is not guaranteed.

Some database engines may add rows that are physically where you would expect them to be, but others may not, and deletions or updates may affect this order, too.

The point is this: you should always select records based on the data that is contained in the records, never on what you assume is the order of the records on the storage device.

To find the most recently added record from the table, include a field in the table with auto increment.

Then use this querry
$result = mysql_query("SELECT * FROM tablename ORDER BY id DESC limit 0,1");
while($row = mysql_fetch_array( $result )) {
echo $row['field1'];
}

Here 'id' is the field with the autoincrement property. This will display field 1 of the most recently added record.

Want to clear doubts about this code?

Sorting Data in Alphabetical order

<?php
$db = mysql_connect ( "localhost" , "username" , "password" )or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database name");

$sql = "SELECT * FROM tablename order by fieldname" ;
$result = mysql_query ( $sql);
echo "<table border='1'>";


while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";

echo $row['field1'];
echo "</td><td>";
echo $row['field2'];
echo "</td><td>";
echo $row['field3'];
echo "</td></tr>";
}

echo "</table>";
mysql_close($db);
?>

Want to clear doubts about this code?

Getting Data from MySQL

The following code will display the records from the database in the following format.

Field1

Field1 of record1

Field2 of record1

------

Field2

Field2 of record2

Field2 of record2

------

$result = mysql_query("SELECT * FROM tablename ");
$field_count = mysql_num_fields($result);
$row_count = mysql_num_rows($result);

for($i=0;$i<$field_count;$i++)
{
$result = mysql_query("SELECT * FROM tablename ");
$field_name = mysql_field_name($result, $i);
echo $field_name;
echo '</br>';
for($j=0;$j<$row_count;$j++)
{

$record = mysql_fetch_array($result);
echo $record[$field_name];
echo '</br>';

}

}

mysql_free_result($result);
mysql_close($db);

Want to clear doubts about this code?

Format Alternate Rows in Different Colors

//Displays alternate table row colors

function row_color($i)

{

$bg1 = "red"; // color one
$bg2 = "#F7F7F7"; // color two

if ( $i%2 )

{
return $bg1;
}

else

{
return $bg2;
} }

// Database Query

// Starts the table

echo "<table bgcolor=#FFFFFF border=0 cellpadding=1 cellspacing=1>\n";

// Create the contents of the table.
for( $i = 0; $i < $row = mysql_fetch_array($result); $i++){
echo "<TR bgcolor=".row_color($i).">\n"
."<TD>".$row["userid"]."</TD>\n"
."<TD>".$row["email"]."</TD>\n"
."</TR>";
}
echo "</TABLE>"

 

Copyright © 2008 www.worth2read.org. All rights Reserved. Use of this Site is subject to our Privacy Statement

Untitled Document
Programming PHP
Beginning PHP and MySQL: From Novice to Professional, Third Edition (Beginning from Novice to Professional)
Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
PHP and MySQL Web Development (3rd Edition) (Developer's Library)
PHP & MySQL For Dummies 3rd edition (For Dummies (Computer/Tech))
PHP Solutions: Dynamic Web Design Made Easy (Solutions)
Build Your Own Website The Right Way Using HTML & CSS
Learning Web Design: A Beginner's Guide to (X)HTML, StyleSheets, and Web Graphics
Sams Teach Yourself HTML and CSS in 24 Hours (7th Edition) (Sams Teach Yourself)