|
 |
|
มีปัญหามาถามครับ php+mysql จะทำอย่างไรให้สามารถแสดง filed และ data ใน table ได้โดยที่เราไม่ทราบเลยว่า |
|
 |
|
|
 |
 |
|
ในบทเรียนมีอยู่นะครับ
|
 |
 |
 |
 |
Date :
10 ก.พ. 2548 18:41:42 |
By :
@W_IN |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
วิธีสร้างสคริป ดูตรงนี้ครับ
1.ทำการอ่านรายชื่อตารางจากฐานข้อมูล เช่น ในฐานข้อมูล db_test มีรายชื่อตารางดังนี้ tbl_test1,tbl_test2,...tbl_testn => n = จำนวนเต็มตั้งแต่ 1->
คำสั่งที่ใช้เรียกดูตารางในฐานข้อมูล คือ mysql_query("show tables;"); แต่ต้องขอใช้ฐานข้อมูลก่อน mysql_query("use db_test;");
2.ทำการเก็บรายชื่อตารางที่ได้ไว้ในตัวแปร และเขียน-อ่านมาแสดงทีละตาราง
3. ตัวนี้เป็น fuction แสดงข้อมูลโดยเราไม่จำเป็นต้องรู้ชื่อฟิลด์ก็สามารถนำข้อมูลในตารางมาแสดงได้
ฟังก์ชัน show_tbl_data() สำหรับแสดงข้อมูลในตารางโดยไม่ต้องรู้ชื่อฟิลด์แค่เรียกใช้ ดังนี้ show_tbl_data(ชื่อตารางของคุณ)
fucntion show_tbl_data($tblname)
{
$sql="select * from $tblname;";
$result=mysql_query($sql);
$nfield=mysql_num_fields($result);
$nrow=mysql_num_rows($result);
echo "<table width='100%' cellpadding='0' cellspacing='0' border='1' align='center'>";
echo "<tr align='center'>";
for($n=0;$n<$nfield;$n++)
{
$fieldname=mysql_field_name($result,$n);
echo "<td>" . $fieldname . "</td>";
}
echo "</tr>";
while($db=mysql_fetch_array($result))
{
echo "<tr>";
for($n=0;$n<$nfield;$n++)
{
$fieldname=mysql_field_name($result,$n);
echo "<td>" . $db[$fieldname] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
ลองนำไปใช้ดูครับ
|
 |
 |
 |
 |
Date :
12 ก.พ. 2548 14:20:06 |
By :
noom_programmer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สว่างเลยครับขอบคุณ คุณnoom_programmer มากๆเลยครับ
|
 |
 |
 |
 |
Date :
12 ก.พ. 2548 20:21:24 |
By :
BadtzMaru |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
|
 |
 |
 |
 |
Date :
2009-04-26 09:43:02 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|