A simple automatic mail system (III) _ PHP Tutorial

Source: Internet
Author: User
A simple automatic mail system (3 ). A simple automatic mail system (III) describes how to combine php and mysql. How to extract data from a mysql database. Well, we have successfully completed our request for a simple automatic mail system (3)

This section describes how to use php and mysql in combination. How to extract data from a mysql database.

Well, we have successfully completed our requirements. a lot of data already exists in the database. The question now is, how can we query the data and get useful results?

In the following program, we will select "apple" user output.

--------------------------------------------------------



/* Declare some required variables */

$ Hostname = "yourhostname ";
$ Username = "yourusername ";
$ Password = "yourpassword ";
$ Userstable = "information";/* access information of data tables created using MySQL */
$ DbName = "yourdbname ";


/* Connect to the database */
MYSQL_CONNECT ($ hostname, $ username, $ password) or die ("Unable to connect to database ");

@ Mysql_select_db ("$ dbName") or die ("Unable to select database ");


/* Select all "apple" users */

$ Query = "SELECT * FROM $ userstable WHERE (preference LIKE 'append ')";

$ Result = MYSQL_QUERY ($ query );

/* Count the number of such users */

$ Number = MYSQL_NUMROWS ($ result );

/* Output result */

$ I = 0;

IF ($ number = 0 ):

PRINT"

Nobody in the database prefers Apples!

";

ELSEIF ($ number> 0 ):

PRINT"

Users preferring Apples: $ number

";

WHILE ($ I <$ number ):

$ Name = mysql_result ($ result, $ I, "name ");
$ Email = mysql_result ($ result, $ I, "email ");

PRINT "Visitor $ name likes Apples.
";
PRINT "Email address: $ email .";
PRINT"

";

$ I ++;

ENDWHILE;

PRINT"

";

ENDIF;


?>

--------------------------------------------------------

Save it as apples. php3

Explanation: some newly used functions:

1. $ number = MYSQL_NUMROWS ($ result );

Syntax: int mysql_num_rows (string result );

· Result returned from the mysql_query function.
· Returns the number of rows that exist in $ result.

2. $ name = MYSQL_RESULT ($ result, $ I, "name ");

Syntax: int mysql_result (int result, int I, column );

This function will separate records and assign each record to the variable.
· $ Result indicates the array result in.
· $ I is the row of the index data.
· Column is the name of a column in the mysql data table. You can also use variables.

Therefore, using a simple while loop, we can easily output data to the browser.

Summary (3) Here we will introduce how php and mysql work together. How to extract data from a mysql database. Well, we have successfully completed what we want...

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.