|
 |
|
พอดีต้องการดึงข้อมูล 2 ตารางมาแสดงหน้าเว็บทำไงเหรอครับ แต่ติดปัญหาไม่รู้จะเขียนยังไงดีครับ |
|
 |
|
|
 |
 |
|
พอดีต้องการดึงข้อมูลจากตาราง 2 ตารางมาแสดงหน้าเว็บแต่ติดปัญหาไม่รู้จะเขียนยังไงดีครับ
ฐานข้อมูล
Code (SQL)
CREATE TABLE `question` (
`Question_Id` int(3) NOT NULL auto_increment,
`Question_Detail` text NOT NULL,
`Question_Type` enum('S','M') NOT NULL default 'S',
PRIMARY KEY (`Question_Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
--
-- dump ตาราง `question`
--
INSERT INTO `question` VALUES (3, '1+1', 'S');
INSERT INTO `question` VALUES (4, '2+2', 'S');
CREATE TABLE `choice` (
`Choice_Id` int(3) NOT NULL auto_increment,
`Choice` varchar(255) NOT NULL,
`Point` float NOT NULL default '0',
`Question_Id` varchar(3) NOT NULL,
PRIMARY KEY (`Choice_Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
--
-- dump ตาราง `choice`
--
INSERT INTO `choice` VALUES (1, '1', 1, '3');
INSERT INTO `choice` VALUES (2, '2', 0, '3');
INSERT INTO `choice` VALUES (3, '3', 0, '3');
INSERT INTO `choice` VALUES (4, '4', 0, '3');
INSERT INTO `choice` VALUES (5, '7', 0, '4');
INSERT INTO `choice` VALUES (6, '8', 0, '4');
INSERT INTO `choice` VALUES (7, '11', 0, '4');
code
Code (PHP)
<?
$host="localhost";
$username="root";
$password="1234";
$db="test";
mysql_connect( $host,$username,$password) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
$sql="select * from question";
$db_query=mysql_query($sql);
while($result=mysql_fetch_array($db_query))
{$i=$i+1;
$qig = $result["Question_Id"];
$question =$result["Question_Detail"];
echo ($qig)."/".($question)."</br>";
}
$sql = "select * from choice where Question_Id";
$db_query=mysql_query($sql);
while($result=mysql_fetch_array($db_query))
{
$cid = $result["Choice_Id"];
$choice = $result["Choice"];
echo ($cid)."-".($choice)."</br>";
}
?>
อยากให้แสดงอย่างนี้ครับ
1.คำถาม
ตัวเลือกที่ 1
ตัวเลือกที่ 2
2..คำถาม
ตัวเลือกที่ 1
ตัวเลือกที่ 2
ฐานข้อมูลใช้แยกกันน่ะครับไม่ทราบพอมีวิธีไหม
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2012-12-22 22:51:28 |
By :
dannan882 |
View :
1043 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
select c.Choice , c.Question_Id from choice c , question q where q.Question_Id = c.Question_Id order by c.Question_Id
|
 |
 |
 |
 |
Date :
2012-12-22 23:48:55 |
By :
kokiflam |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
$host="localhost";
$username="root";
$password="1234";
$db="test";
mysql_connect( $host,$username,$password) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");
$sql = "select * from question q, choice c where q.Question_Id = c.Question_Id order by c.Question_Id";
$rs = mysql_query($sql);
$i = 0;
while($row = mysql_fetch_object($rs))
{
if($i == 0)
{
$value = $row->Question_Id;
echo $row->Question_Id." " . $row->Question_Detail. "<br>";
}
if($row->Question_Id == $value)
{
echo $row->Choice."<br>";
$i++;
}
else if($row->Question_Id != $value)
{
$value = $row->Question_Id;
echo $row->Question_Id." " . $row->Question_Detail. "<br>";
echo $row->Choice."<br>";
$i = 1;
}
}
?>

|
 |
 |
 |
 |
Date :
2012-12-23 00:10:53 |
By :
kokiflam |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากๆๆครับ
|
 |
 |
 |
 |
Date :
2012-12-23 00:52:39 |
By :
dannan882 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คำว่า "ฐานข้อมูลใช้แยกกันน่ะครับไม่ทราบพอมีวิธีไหม"
ฐานข้อมูลใช้แยกกัน ใช้คำผิดหรือเปล่าครับ แบบนี้เรียกว่าใช้ฐานข้อมูลเดียวกัน แต่คนละตาราง
|
 |
 |
 |
 |
Date :
2012-12-23 01:13:11 |
By :
chaynuwong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|