|
 |
|
ต้องการ select ข้อมูลมากกว่า 1 ตาราง OOP ผู้รู้ตอบทีครับ |
|
 |
|
|
 |
 |
|
ไฟล์ config.inc.php
<?php
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PASS","root");
define("DB_NAME","test");
?>
ไฟล์ class_db.inc.php
Code (PHP)
<?php
class Class_DB
{
private $charset = "UTF8";
private $rs;
private $_fetch_array = array();
public function __construct($host,$user,$pass,$db_name)
{
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_query("SET NAMES ".$this->charset);
mysql_query("USE $db_name");
}
public function query($strsql) //รับคำสั่ง sql เข้ามา query
{
$this->rs= mysql_query($strsql);
}
public function num_rows() //เช็คจำนวนแถว
{
return mysql_num_rows($this->rs);
}
//---mysql fetch array()--//
public function fetch_array()
{
if(count($this->_fetch_array)>0)
{
return $this->_fetch_array;
}
else
{
while($row=mysql_fetch_array($this->rs))
{
$this->_fetch_array[] = $row;
}
return $this->_fetch_array;
}
}
//---End mysql fetch array() ---//
public function fetch_row()
{
return mysql_fetch_row($this->rs);
}
}
?>
ไฟล์ index.php
<?php
session_start(); //เพื่อใช้งาน session
include_once("lib/config.inc.php");
include_once("lib/class_db.inc.php");
$db = new Class_DB(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$db->query("select banner_url from advertise");
echo "จำนวนแถวมของ tb_user คือ: ".$db->num_rows();
while(list($user_id)=$db->fetch_row()) {
echo "<p>ข้อมูล: ".$user_id. "</p>";
}
?>
==========คำถามของผมก็คือ============
ผมต้องการ select ตารางที่ 2 หรือ ตารางที่ 3 ต้องทำยังไงคับ รบกวนผู้รู้ช่วยตอบทีนะครับ
เช่นตัวอย่างด้านบน มีการ select จาก advertise แล้ว หากผมต้องการใช้งานข้อมูลจากตารางอื่นต้องเขียนโค้ด หรือแก้โค้ดตรงไหนครับ
ขอบคุณมากๆ คับ
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2014-04-06 11:58:20 |
By :
unknow_newbaby |
View :
753 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
[b]ลองศึกษาดูน่ะครับ[/b]
คำสั่ง sql Select ข้อมูลสองตาราง Mysql ใช้ได้ กับ Sql มาตรฐาน ใช้การ Joins Left Join, Right Join, Inner Join
ตัวอย่างมีตารางข้อมูล
ตาราง products;
+----+--------------+--------------+
| id | product_name | manufacturer |
+----+--------------+--------------+
| 1 | Shoes | Company1 |
| 2 | Laptop | Company2 |
| 3 | Monitor | Company3 |
| 4 | DVD | Company4 |
+----+--------------+--------------+
ตาราง buyers;
+----+------+------------+----------+
| id | pid | buyer_name | quantity |
+----+------+------------+----------+
| 1 | 1 | Steve | 2 |
| 2 | 2 | John | 1 |
| 3 | 3 | Larry | 1 |
| 4 | 3 | Michael | 5 |
| 5 | NULL | Steven | NULL |
+----+------+------------+----------+
การ Join ตารางแบบ Left Join เป็นการค้นหาข้อมูลจากสองตารางโดยยึดตาราง ทางซ้ายเป็นหลัก ในที่นี่ตารางซ้ายคือ buyers ต้องมีข้อมูล
SELECT buyer_name, quantity, product_name FROM buyers LEFT JOIN products ON
buyers.pid=products.id;ผล
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
| Steven | NULL | NULL |
+------------+----------+--------------+
การ Join ตาราง แบบ Right Join
เป็นการค้นหาข้อมูลจากสองตารางโดยยึดตาราง ทางซ้ายเป็นหลัก ในที่นี่ตารางขวาคือ products ต้องมีข้อมูล
SELECT buyer_name, quantity, product_name FROM buyers RIGHT JOIN products ON
buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
| NULL | NULL | DVD |
+------------+----------+--------------+
การ Join ตารางแบบ Inner join ข้อมูลที่จะออกมาต้องมีทั้งในตาราง ซ้ายและขวา เครื่องหมาย= (buyers.pid=products.id)
SELECT buyer_name, quantity, product_name FROM buyers INNER JOIN products ON
buyers.pid=products.id;ลองเอาไปรันดูผล
|
 |
 |
 |
 |
Date :
2014-04-06 12:36:50 |
By :
new19121 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|