我们如何借助PHP脚本显示MySQL表中的所有记录?
为了说明这一点,在下面的示例中,我们使用PHP脚本从名为 “Tutorials_tbl”的表中获取所有记录,该脚本使用mysql_query()和mysql_fetch_array()函数-
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT tutorial_id, tutorial_title, tutorial_author, submission_date
FROM tutorials_tbl';
mysql_select_db('TUTORIALS');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "Tutorial ID :{$row['tutorial_id']} <br> ".
"Title: {$row['tutorial_title']} <br> ".
"Author: {$row['tutorial_author']} <br> ".
"Submission Date : {$row['submission_date']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>在上面的示例中,常量MYSQL_ASSOC用作PHP函数 mysql_fetch_array()的第二个参数,因此它将行作为关联数组返回。使用关联数组,可以使用字段名称而不是使用索引来访问该字段。
热门推荐
10 发给女儿生日简短祝福语
11 老师新婚快乐祝福语简短
12 周二祝福语大全简短
13 宝宝100日简短祝福语
14 表达家人的祝福语简短
15 感动妈妈的祝福语简短
16 中秋祝福语简短版大全
17 简短搞笑的虎年祝福语
18 妈妈生日贺卡祝福语 简短