Home > Backend Development > PHP Tutorial > A simple automatic email sending system (3)_PHP tutorial

A simple automatic email sending system (3)_PHP tutorial

WBOY
Release: 2016-07-21 16:05:22
Original
822 people have browsed it

A simple automatic email sending system (3)

Here we introduce the practical combination of php and mysql. How to extract data from mysql database.

Okay, we have successfully completed our requirements. A lot of data already exists in the database. The question now is, how to query this data and get useful results?

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

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



/* Declare some necessary variables*/

$hostname = "yourhostname ";
$username = "yourusername";
$password = "yourpassword";
$userstable = "information"; /* Use the data table created by MySQL to access information */
$dbName = "yourdbname";


/* Connect to 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 'Apples') ";

$result = MYSQL_QUERY($query);

/* Count how many such users there are*/

$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 The array record returned from the function mysql_query.
·Return the number of rows stored in $result.

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

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

This function will separate the records and assign each one to a variable.
·$result refers to the array result in.
·$i refers to the row of data.
·column refers to the name of the column in the mysql data table. Variables can also be used.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315719.htmlTechArticleA simple automatic email sending system (3) Here we introduce the practical combination of php and mysql. How to extract data from mysql database. Okay, we have successfully completed what we want...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template