PHP displays MySQL database data from a row (13) using PHP

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.