一个简单的自动发送邮件系统(三)_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循环,我们就能很容易的将数据输出给浏览器。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
