A simple automatic mail system (III) & nbsp; php (as the mainstream development language) and MySQL (the best combination with PHP. How to extract data from MySQL (the best combination with PHP) databases. & Nbsp; well, we have a simple automatic email sending system (3)
Here we will introduce how php (as the mainstream development language) works with MySQL (the best combination with PHP. How to extract data from MySQL (the best combination with PHP) databases.
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";/* Data table access information created using MySQL (the best combination with PHP */
$ DbName = "yourdbname ";
/* Connect to the database */
MySQL (best combination with PHP) _ CONNECT ($ hostname, $ username, $ password) or die ("Unable to connect to database ");
@ MySQL (best combination with PHP) _ select_db ("$ dbName") or die ("Unable to select database ");
/* Select all "apple" users */
$ Query = "SELECT * FROM $ userstable WHERE (preference LIKE Apples )";
$ Result = MySQL (the best combination with PHP) _ QUERY ($ query );
/* Count the number of such users */
$ Number = MySQL (the best combination with PHP) _ 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 (the best combination with PHP) _ result ($ result, $ I, "name ");
$ Email = MySQL (the best combination with PHP) _ result ($ result, $ I, "email ");
PRINT "Visitor $ name likes Apples.
";
PRINT "Email address: $ email .";
PRINT"
";
$ I ++;
ENDWHILE;
PRINT"
";
ENDIF;
?>
--------------------------------------------------------
Save it as apples. php (as the mainstream development language) 3
Explanation: some newly used functions:
1. $ number = MySQL (the best combination with PHP) _ NUMROWS ($ result );
Syntax: int MySQL (the best combination with PHP) _ num_rows (string result );
· Result is returned from the array records returned by the _ query function MySQL (the best combination with PHP.
· Returns the number of rows that exist in $ result.
2. $ name = MySQL (the best combination with PHP) _ RESULT ($ result, $ I, "name ");
Syntax: int MySQL (the best combination with PHP) _ 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 a data table in MySQL (the best combination with PHP. You can also use variables.
Therefore, using a simple while loop, we can easily output data to the browser.