Executing a SELECT query command in a PHP script is also called the mysql_query () function, but unlike executing DML, the return value of the mysql_query () function is a reference pointer (result set) of a resource after the Select command is executed. It needs to be handled by the corresponding function.
Mysql_num_rows ($result) Get the number of data records from the result set
Mysql_num_fields ($result) Get the number of data record columns from the result set
Mysql_fetch_row () The function returns a result record and saves it as a normal indexed array
MYSQL_FETCH_ASSOC () The function returns a result record and saves it as a normal associative array
Mysql_fetch_array () is saved by default at the same time as an indexed array and an associative array, three parameters Mysql_assoc, Mysql_num, Mysql_both
Mysql_fetch_object () Returns a result record in the form of an object whose fields need to be accessed as objects
Note: Try not to use the Mysql_fetch_array () method, less efficient
<?PHP$link=mysql_connect(' localhost ', ' root ', ' abc123 '); if(!$link){ die(' Database link failed: '.Mysql_error()); } mysql_select_db(' Test '); $select= "SELECT * from book;"; $result=mysql_query($select); Echo"<table align= ' center ' width= ' 60% ' border= ' 1 ' >"; Echo' <caption>; Echo' <th> book number </th><th> book name </th><th> author </th> '; while($row=Mysql_fetch_row($result)){ Echo"<tr>"; foreach($row as $data){ Echo' <td> '.$data.‘ </td> '; } Echo' </tr> '; } Echo"</table>"; Mysql_free_result($result);//to free the result set resource for a query Mysql_close($link);?>
PHP-Working with the Select query result set in a PHP script