search
Homephp教程php手册一个简单的自动发送邮件系统(三)_php基础

一个简单的自动发送邮件系统(三)

    这里介绍php和mysql结合起来实用。如何从mysql数据库中提取数据。

    好,我们已经成功的完成了我们的要求,很多的数据已经存在了数据库中,现在的问题是,如何查询这些数据,得到有用的结果呢?

在下面的程序中,我们将选择"apple"的用户输出。

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




/* 声明一些必须的变量*/

$hostname = "yourhostname";
$username = "yourusername";
$password = "yourpassword";
$userstable = "information";  /* 使用MySQL建立的数据表存取信息  */
$dbName = "yourdbname";


/* 与数据库连接*/
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");

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


/* 选择所有“apple”用户*/

$query = "SELECT * FROM $userstable WHERE (preference LIKE 'Apples') ";

$result = MYSQL_QUERY($query);

/* 统计有多少这样的用户*/

$number = MYSQL_NUMROWS($result);

/* 输出结果*/

$i = 0;

IF ($number == 0) :

    PRINT "

Nobody in the database prefers Apples!

";

ELSEIF ($number > 0) :

    PRINT "

Users preferring Apples: $number

";

    WHILE ($i
        $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;


?>

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

将他存为apples.php3

解释说明:一些新用到的函数:

1、$number = MYSQL_NUMROWS($result);

语法:int mysql_num_rows(string result);

·result   从函数mysql_query中返回的数组记录。
·返回存在$result中的行数。

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

语法: int mysql_result(int result, int i, column);

这个函数将分离记录,将每一条赋值给变量。
·$result是指中的数组结果。
·$i是指数据的行。
·column是指mysql数据表中列的名字。也可以使用变量。

   因此使用一个简单的while循环,我们就能很容易的将数据输出给浏览器。
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.