A simple automated mail system (iii) _php tutorial

Source: Internet
Author: User
A simple auto-sending mail system (III)

Here is the introduction of PHP and MySQL together practical. How to extract data from the MySQL database.

Well, we have successfully completed our requirements, a lot of data already exist in the database, now the question is how to query the data, to get useful results?

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

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



/* Declare some required variables */

$hostname = "Yourhostname";
$username = "YourUserName";
$password = "YourPassword";
$userstable = "Information"; /* Access information using MySQL-built data tables */
$dbName = "Yourdbname";


/* Connect to Database */
Mysql_connect ($hostname, $username, $password) OR die ("Unable-CONNECT to database");

@mysql_select_db ("$dbName") or Die ("Unable to select database");


/* Select All "apple" users */

$query = "SELECT * from $userstable the WHERE (preference like ' Apples ')";

$result = mysql_query ($query);

/* Statistics on how many such users */

$number = Mysql_numrows ($result);

/* Output Results */

$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 him as a apples.php3.

Explanation: Some new functions are used:

1, $number = Mysql_numrows ($result);

Syntax: int mysql_num_rows (string result);

result the array record returned from the function mysql_query.
• Returns the number of rows that exist in the $result.

2, $name = Mysql_result ($result, $i, "name");

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

This function separates the records and assigns each one to the variable.
• $result refers to the array result in.
• $i refers to the rows of the data.
column refers to the name of a column in a MySQL data table. You can also use variables.

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

http://www.bkjia.com/PHPjc/315719.html www.bkjia.com true http://www.bkjia.com/PHPjc/315719.html techarticle a simple auto-send mail system (iii) Here is the introduction of PHP and MySQL together practical. How to extract data from the MySQL database. Well, we have successfully completed our 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.