Create a PHP Recordset

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);
 
?>

Now that we have the information pulled from the database, we display it on 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>

Now that the page has been built with the data from the database, we clear our recordset and close the connection.

<?php
 
//Close the Recordset and Connection
 
mysql_free_result($Recordset1);
mysql_close($connect);
?>