การอ่านข้อมูลจากตารางในฐานข้อมูล MySQL มาแสดง
ชื่อฟิลด์
|
ประเภท
|
ขนาด
|
ความหมาย
|
stdid | varchar | 7 | รหัสนักศึกษา |
stdname | varchar | 50 | ชื่อ-สกุลนักศึกษา |
<html>
<head>
<title> การอ่านข้อมูลจาก MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
</head>
<body> <?php //------------ส่วนการเปิด Connection ในการติดต่อกับ MySQL ---------------- $hostname = "localhost"; $username = "<ใส่ Username ของ MySQL>"; $password = "<ใส่รหัสผ่าน>"; $dbname = "school";$conn = mysql_connect( $hostname, $username, $password );
if ( ! $conn )
die ( "ไม่สามารถติดต่อกับ MySQL ได้" ); //----------จบ ส่วนการเปิด Connection ในการติดต่อกับ MySQL ---------------
//----------ส่วนการเลือกฐานข้อมูล school -------------------------------------- mysql_select_db ( $dbname, $conn )
or die ( "ไม่สามารถเลือกฐานข้อมูล school ได้" ); //----------จบส่วนการเลือกฐานข้อมูล school -----------------------------------
$sqltxt = "SELECT * FROM student";$result = mysql_query ( $sqltxt, $conn );
while ( $rs = mysql_fetch_array ( $result ) )
{
echo "$rs[stdid] - $rs[stdname]<br>";
}//----------ส่วนการปิด Connection ในการติดต่อกับ MySQL -------------------- mysql_close ( $conn ); //----------จบส่วนการปิด Connection ในการติดต่อกับ MySQL------------------ ?> </body>
</html>
<html>
<head>
<title>การอ่านข้อมูลจาก MySQL ลงตาราง</title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
</head>
<body>
<table width="500" border="1" cellpadding="2" cellspacing="0" bordercolor="#0066FF">
<tr bgcolor="#0066FF">
<td><div align="center"><font color="#FFFFFF" face="Tahoma, MS Sans Serif"><strong>รหัสนักศึกษา</strong></font></div></td>
<td><div align="center"><font color="#FFFFFF" face="Tahoma, MS Sans Serif"><strong>ชื่อ
- สกุล</strong></font></div></td>
</tr>
<?php
$hostname = "localhost"; $username = "<ใส่ Username ของ MySQL>"; $password = "<ใส่รหัสผ่าน>"; $dbname = "school";$conn = mysql_connect( $hostname, $username, $password );
if ( ! $conn )
die ( "ไม่สามารถติดต่อกับ MySQL ได้" );
mysql_select_db ( $dbname, $conn )
or die ( "ไม่สามารถเลือกฐานข้อมูล school ได้" );
$sqltxt = "SELECT * FROM student";$result = mysql_query ( $sqltxt, $conn );
while ( $rs = mysql_fetch_array ( $result ) )
{ ?> <tr>
<td><font size="2" face="Tahoma, MS Sans Serif"><?php echo $rs["stdid"] ?></font></td>
<td><font size="2" face="Tahoma, MS Sans Serif"><?php echo $rs["stdname"] ?></font></td>
</tr>
<?php
} mysql_close ( $conn ); ?> </table>
</body>
</html>
ตัวอย่างแรกจะใช้ $rs[stdid]
ตัวอย่างที่ 2 จะใช้ $rs["stdid"]
0 ความคิดเห็น :
แสดงความคิดเห็น
Click to see the code!
To insert emoticon you must added at least one space before the code.