Blog

Newest Post
Blog Archives
Blog Categories

Portfolio

3D Models
Banners
Brochures
Business Cards
Custom Programming
Environments
Flash Projects
Game Design
Image Altering
Web Design

About

Resume
Recommendation Letters

Contact

November 20, 2007

PHP Recordsets

Filed under: PHP — admin @ 8:07 pm

Recordsets in PHP are useful bits of code used to display data that is stored within a MYSQL table. You can run a loop to display all of this data or you can display only the bit of information you want. Here is how I do this:

<?php
//First Connect to the Database
 
$hostname_connect = "localhost";
$database_connect = "db_name";
$username_connect = "db_user";
$password_connect = "db_password";
$connect = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
 
//Create the Recordset and grab the data you want
 
mysql_select_db($database_connect, $connect);
$query_Recordset1 = sprintf("SELECT * FROM names WHERE name='steve'");
$Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
 
?>
 
<!–Display the data within the page –>
<body>
 
<p>Hello <?php echo $row_Recordset1['name']; ?>, you live on <?php echo $row_Recordset1['street']; ?>. You have <?php echo $row_Recordset1['kids'] ?> kids.</p>
 
</body>
 
<?php
 
//Close the Recordset and Connection
 
mysql_free_result($Recordset1);
mysql_close($connect);

The above example grabbed data from the MYSQL table “names” only where the row’s name was “Steve”. Then data is displayed about Steve in each of the rows within the table; such as address and kids.

Leave a Reply

© - Digital Epiphany Designs, Online Portfolio for Web Design, Custom Programming, Grapic Design and Game Design.