Once a query has been made and the result set has been assigned to an array variable, we are now able to view the results.


Since the result set is returned as an array, we need to traverse through the array by passing this array variable as a parameter in to the mysql_fetch_array() function.


mysql_fetch_array( $resultSet, MYSQL_ASSOC ) ;


*Note: An optional second parameter of MYSQL_ASSOC or MYSQL_NUM specifies either an associative or numerical indexed array. Or, by NOT including the optional second parameter the default MYSQL_BOTH parameter is used to work upon either associative or numerical indexes in subsequent array traversals.


* alternatively, the mysql_fetch_assoc() function provides an associative array.


The advantage of using associative arrays, is that the field names can be used whilst looping through the array.



	$connection = mysql_connect( $server, $user, $passwd ) ;

	if( ! $connection ) {

		die('Error connecting to the database:' . mysql_error() ) ;

	mysql_select_db('Academy') ;

	$query = 'SELECT * FROM customers' ;

	$resultSet = mysql_query( $query );

	if( ! $resultSet){

		die('Error getting result set: ' . mysql_error() ) ;

	while( $record = mysql_fetch_array( $resultSet ) ) {

		echo "CustID: {$record[CustID]}, " .     //double quoted string using {} to interpolate record
			 'First: ' . $record[First] . ', ' . //single quoted string using concatenated record
			 "Surname: {$record[Surname]}, " .   //these different methods have just been
			 'Age: ' . $record[Age]. '<br>' ;    //shown as a different way to do things

	mysql_close( $connection ) ;


CustID: 1, First: Pam, Surname: Frost, Age: 21
CustID: 2, First: James, Surname: Cross, Age: 20
CustID: 3, First: Andy, Surname: Jones, Age: 34


Leave a Reply