Friday, July 26, 2013

Display text from database with read more link

Display text from database with read more link

Retrieve data from a table

Address book



1st we create two files list.php, person.php with the following code:

Code for list.php
<html>
 <body>
 <ul>
 <?php
 mysql_connect("localhost", "admin", "abc123") or die (mysql_error ());
 mysql_select_db("mydb") or die(mysql_error());
 $rs = mysql_query('SELECT * FROM mytable');
while($result = mysql_fetch_array($rs)){ 

   echo $result['firstname'];
   echo $result['lastname'];
echo "<a href='person.php?id=".$result['id']."'> More... </a>";
echo"<br>";
}
 mysql_close();
 ?>
 </ul>
 </body>
 </html>
code for person.php
<html>
 <body>
 <dl>
 <?php
 mysql_connect("localhost", "admin", "abc123") or die (mysql_error ());
 mysql_select_db("mydb") or die(mysql_error());
 
 $strSQL = "SELECT * FROM mytable WHERE ID=" . $_GET["id"];
 $rs = mysql_query($strSQL);
 
 // Loop the recordset $rs
 while($row = mysql_fetch_array($rs)) {

  // Write the data of the person
  echo "<dt>Name:</dt><dd>" . $row["firstname"] . " " . $row["lastname"] . "</dd>";
  echo "<dt>Phone:</dt><dd>" . $row["Contactno"] . "</dd>";
  echo "<dt>Birthdate:</dt><dd>" . $row["dateofbirth"] . "</dd>";
 }
 mysql_close();
 ?>
 </dl>
 <p><a href="list.php">Return to the list</a></p>
 </body>
 </html> 

0 Comments:

Post a Comment