1. Execute SQL statements in PHP
To obtain data from the database, PHP must first execute an SQL statement for table operations, including SELECT, INSERT, UPDATE, or DELETE statements. Generally, when you execute the SELECT statement in PHP, some record rows will be searched from the table. When other statements are executed, only information about whether the statements are successfully executed is returned.
Success '; $ num = mysql_num_rows ($ result); // obtain the number of rows in the query result echo'
Select'. $ Num .'Rows ';} mysql_close ($ conn);?>
Generally, mysql_query () is used together with mysql_error () to find the cause of the problem based on the information generated by mysql_error () When an error occurs in SQL statement execution.
ERROR:".mysql_error()."
Problem:
.$sql");if($result){echo 'SQLsyntex:'.$sql.'
Success';$num=mysql_num_rows($result);echo '
select '.$num.' rows';}mysql_close($conn);?>
2. Use PHP to process data result sets
After an SQL statement is successfully executed in a program, you can use mysql_fetch_array () to obtain the specific query result. That is, you can use this function to obtain the field value of the record.
ERROR:". Mysql_error ()."
Problem:
. $ SQL "); if ($ num = mysql_num_rows ($ result) {$ row = mysql_fetch_array ($ result); echo'';print_r($row);}mysql_close($conn);?>
The mysql_fetch_array () function describes a row returned by the function in the result set and uses it as an associated array or a common array. By default, the returned array is both. The array returned by mysql_fetch_array () creates two indexes for each field value: one is a number index, and the other is a field name index.
ERROR:". Mysql_error ()."
Problem:
. $ SQL "); if ($ num = mysql_num_rows ($ result) {$ row = mysql_fetch_array ($ result); echo'';while($row=mysql_fetch_array($result,MYSQL_ASSOC)){print_r($row);}}mysql_close($conn);?>
Use the while loop to call the function mysql_fetch_array () multiple times, assign the returned array to the variable $ row each time, and then output the array variable $ row in the loop body. When calling the function mysql_fetch_array (), specify the second parameter as MYSQL_ASSOC. Therefore, the returned result set group is an associated array indexed by field name.