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...