PHP Recordsets
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:
View Code PHP
<?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.




