ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programming>> สอบถาม SQL การ select ข้อมูล <<<
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: >> สอบถาม SQL การ select ข้อมูล <<<  (อ่าน 1370 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
bankker6480
สมุนแก๊งเสียว
*

พลังน้ำใจ: 162
ออฟไลน์ ออฟไลน์

กระทู้: 710



ดูรายละเอียด เว็บไซต์
« เมื่อ: 19 กุมภาพันธ์ 2013, 17:59:50 »

ตัวอย่างตาราง
http://pic.free.in.th/id/87053bf820fa35ce1ecd0676f41a6dc9

ผมต้องการดึงข้อมูลจาก ฟิล์ด Category เอาข้อมูล Phone เพียงอย่างเดียวออกมาแสดงที่หน้า Page
ต้องเขียนยังไงครับ ช่วยด้วยครับ wanwan017
บันทึกการเข้า
thatthep
Newbie
*

พลังน้ำใจ: 2
ออฟไลน์ ออฟไลน์

กระทู้: 29



ดูรายละเอียด
« ตอบ #1 เมื่อ: 19 กุมภาพันธ์ 2013, 18:03:26 »

โค๊ด:
select * from table_x where Category='Phone'
บันทึกการเข้า
gubaaball
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 184
ออฟไลน์ ออฟไลน์

กระทู้: 1,678



ดูรายละเอียด เว็บไซต์
« ตอบ #2 เมื่อ: 19 กุมภาพันธ์ 2013, 18:03:50 »

โค๊ด:
$sql="select Category from ชื่อตาราง where Category = 'Phone'";
$res=mysql_query($sql);
while($arr=mysql_fetch_assoc($res)){
  echo $arr['Category'].'<br />';
}


อย่างนี้ป่ะครับ ผิดพลาดยังไงก็ขออภัยด้วย  Cry
บันทึกการเข้า
p44n
Verified Seller
สมุนแก๊งเสียว
*

พลังน้ำใจ: 105
ออฟไลน์ ออฟไลน์

กระทู้: 676



ดูรายละเอียด
« ตอบ #3 เมื่อ: 19 กุมภาพันธ์ 2013, 18:26:00 »

การเก็บข้อมูล แบบ enum เป็นการ fixed ค่าที่ต้องการให้เก็บ ข้อดีตรงนี้ไม่สำคัญ เพราะเราสามารถคอนโทรลค่าก่อนเก็บเองได้
แต่ข้อดีที่สุดคือ enum มันประหยัดเนื้อที่มากๆ

การดึงก้อให้เรามอง enum เป็น text ธรรมดาๆคับ ฉะนั้นแล้วก้อดึงได้เลยตามท่านด้านบนบอกคับ

 wanwan017
บันทึกการเข้า

Python, PHP OOP, MVC CodeIgniter, MongoDB, MySQL, MsSQL ,CSS Tableless, Responsive Design, Cross-platform, Javascript, jQuery, AngularJS, NodeJS,  Fackbook API, Paypal API, Omise API, Google Map API, C# Window Applications, .NET Framework, JAVA (Android Developer, JSP), Hybrid Application with Cordova + Ionic Framework, ReactJS, React Native
bankker6480
สมุนแก๊งเสียว
*

พลังน้ำใจ: 162
ออฟไลน์ ออฟไลน์

กระทู้: 710



ดูรายละเอียด เว็บไซต์
« ตอบ #4 เมื่อ: 19 กุมภาพันธ์ 2013, 18:31:47 »

ดึงออกมาเป็นแบบนี้อ่าครับ
คือจำนวนถูก แต่ดึงข้อมูลออกมาไม่ได้
http://image.ohozaa.com/view2/wENoVEAPyvOMT7Rr
บันทึกการเข้า
p44n
Verified Seller
สมุนแก๊งเสียว
*

พลังน้ำใจ: 105
ออฟไลน์ ออฟไลน์

กระทู้: 676



ดูรายละเอียด
« ตอบ #5 เมื่อ: 19 กุมภาพันธ์ 2013, 18:34:22 »

ดึงออกมาเป็นแบบนี้อ่าครับ
คือจำนวนถูก แต่ดึงข้อมูลออกมาไม่ได้
http://image.ohozaa.com/view2/wENoVEAPyvOMT7Rr


ถ้าจำนวนถูก ก็แสดงว่าดึงได้แล้ว

น่าจะมีปัญหาในการแสดงผลมากกว่าคับ ลอง check ตัวแปร, ลอง trim, ลองๆๆ หลายๆ วิธีคับ
บันทึกการเข้า

Python, PHP OOP, MVC CodeIgniter, MongoDB, MySQL, MsSQL ,CSS Tableless, Responsive Design, Cross-platform, Javascript, jQuery, AngularJS, NodeJS,  Fackbook API, Paypal API, Omise API, Google Map API, C# Window Applications, .NET Framework, JAVA (Android Developer, JSP), Hybrid Application with Cordova + Ionic Framework, ReactJS, React Native
bankker6480
สมุนแก๊งเสียว
*

พลังน้ำใจ: 162
ออฟไลน์ ออฟไลน์

กระทู้: 710



ดูรายละเอียด เว็บไซต์
« ตอบ #6 เมื่อ: 19 กุมภาพันธ์ 2013, 18:38:39 »

Phone.php
โค๊ด:
<?
include("includes/db.php");
include("includes/functions.php");

if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="table.inc.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>สินค้าทั้งหมด</title>
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
<style type="text/css">
body {
<!--background-color: #E7EBF2;-->
background-image:url('../images/body1.jpg');

background-repeat : no-repeat;
background-attachment: fixed;
background-position:top center;
background-image: url(http://bank-network.info/images/body1.jpg);
}
</style><link href="mytopic.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="table.inc.css">
</head>
<body>

<center>
 <table width="1000" border="0">
 <thead>
<tr>
    <td><img src="http://bank-network.info/images/logo.png" width="260" height="100" /></td>
  </tr>
<tr>
    <td><? include("menu.php"); ?></td>
  </tr>
  <tr>
    <td height="300"><img src="http://bank-network.info/images/flash.png" width="1000" height="300" /></td>
  </tr>
 
<tr>
    <td class="topic">สินค้าทั้งหมด</td>
</tr>   
</thead>

<form name="form1">
<input type="hidden" name="productid" />
    <input type="hidden" name="command" />
</form>

<div align="center">

<table id="for# " class="simply th">

<tr>
<td>
<form id="form3" name="form3" method="post" action="search.php">
<FONT COLOR=#000000>ค้นหาสินค้า </FONT><input name="txt_search" type="text" value="<?=$_POST['txt_search']?>" /> <input name="Search" type="submit" value="ค้นหา" />
</form>
</td>
</tr>


<!--<table border="0" cellpadding="2px" width="600px">-->
<?
$result=mysql_query("select Category from files where Category = 'Phone'");
while($row=mysql_fetch_assoc($result)){
?>
<tbody>
    <tr>
        <td><center><img src="images/<?=$row['FilesName']?>" width="200" height="220" /></center></td>
            <td>    <b><?=$row['Name']?></b><br />
            <?=$row['Detail']?><br />
<b>Product ID :</b><?=$row['FilesID']?><br />
                    <b>Price:</b><big style="color:green">
                    $<?=$row['Price']?></big><br /><br />
                    <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['FilesID']?>)" />
</td>
</tr>
        <? } ?>
</tbody>
    </table>
</div>
<tfoot>
  <tr>
    <center><td height="118"><img src="http://bank-network.info/images/footer.png" width="1000" height="114" /></td></center>
  </tr>
  </tfoot>
</table>
</center>
<script type="text/javascript">
function popup(url,name,windowWidth,windowHeight){   
myleft=(screen.width)?(screen.width-windowWidth)/2:100;
mytop=(screen.height)?(screen.height-windowHeight)/2:100;
properties = "width=" windowWidth ",height=" windowHeight;
properties  =",scrollbars=yes, top=" mytop ",left=" myleft;   
window.open(url,name,properties);
}
</script>
</body>
</html>
บันทึกการเข้า
gubaaball
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 184
ออฟไลน์ ออฟไลน์

กระทู้: 1,678



ดูรายละเอียด เว็บไซต์
« ตอบ #7 เมื่อ: 19 กุมภาพันธ์ 2013, 18:41:27 »

Phone.php
โค๊ด:
<?
include("includes/db.php");
include("includes/functions.php");

if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="table.inc.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>สินค้าทั้งหมด</title>
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
<style type="text/css">
body {
<!--background-color: #E7EBF2;-->
background-image:url('../images/body1.jpg');

background-repeat : no-repeat;
background-attachment: fixed;
background-position:top center;
background-image: url(http://bank-network.info/images/body1.jpg);
}
</style><link href="mytopic.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="table.inc.css">
</head>
<body>

<center>
 <table width="1000" border="0">
 <thead>
<tr>
    <td><img src="http://bank-network.info/images/logo.png" width="260" height="100" /></td>
  </tr>
<tr>
    <td><? include("menu.php"); ?></td>
  </tr>
  <tr>
    <td height="300"><img src="http://bank-network.info/images/flash.png" width="1000" height="300" /></td>
  </tr>
 
<tr>
    <td class="topic">สินค้าทั้งหมด</td>
</tr>   
</thead>

<form name="form1">
<input type="hidden" name="productid" />
    <input type="hidden" name="command" />
</form>

<div align="center">

<table id="for# " class="simply th">

<tr>
<td>
<form id="form3" name="form3" method="post" action="search.php">
<FONT COLOR=#000000>ค้นหาสินค้า </FONT><input name="txt_search" type="text" value="<?=$_POST['txt_search']?>" /> <input name="Search" type="submit" value="ค้นหา" />
</form>
</td>
</tr>


<!--<table border="0" cellpadding="2px" width="600px">-->
<?
$result=mysql_query("select Category from files where Category = 'Phone'");
while($row=mysql_fetch_assoc($result)){
?>
<tbody>
    <tr>
        <td><center><img src="images/<?=$row['FilesName']?>" width="200" height="220" /></center></td>
            <td>    <b><?=$row['Name']?></b><br />
            <?=$row['Detail']?><br />
<b>Product ID :</b><?=$row['FilesID']?><br />
                    <b>Price:</b><big style="color:green">
                    $<?=$row['Price']?></big><br /><br />
                    <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['FilesID']?>)" />
</td>
</tr>
        <? } ?>
</tbody>
    </table>
</div>
<tfoot>
  <tr>
    <center><td height="118"><img src="http://bank-network.info/images/footer.png" width="1000" height="114" /></td></center>
  </tr>
  </tfoot>
</table>
</center>
<script type="text/javascript">
function popup(url,name,windowWidth,windowHeight){   
myleft=(screen.width)?(screen.width-windowWidth)/2:100;
mytop=(screen.height)?(screen.height-windowHeight)/2:100;
properties = "width=" windowWidth ",height=" windowHeight;
properties  =",scrollbars=yes, top=" mytop ",left=" myleft;   
window.open(url,name,properties);
}
</script>
</body>
</html>


เอาข้อมูลทุก field มาแสดงก็ต้องใช้ select * from ครับ ที่ผมยกตัวอย่างเพราะเห็นคำถามว่าต้องการดึงแค่ฟิลด์ Category ลองให้ * ดูครับ ข้อมูลน่าจะมา  :'(
บันทึกการเข้า
bankker6480
สมุนแก๊งเสียว
*

พลังน้ำใจ: 162
ออฟไลน์ ออฟไลน์

กระทู้: 710



ดูรายละเอียด เว็บไซต์
« ตอบ #8 เมื่อ: 19 กุมภาพันธ์ 2013, 18:43:24 »

Phone.php
โค๊ด:
<?
include("includes/db.php");
include("includes/functions.php");

if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="table.inc.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>สินค้าทั้งหมด</title>
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
<style type="text/css">
body {
<!--background-color: #E7EBF2;-->
background-image:url('../images/body1.jpg');

background-repeat : no-repeat;
background-attachment: fixed;
background-position:top center;
background-image: url(http://bank-network.info/images/body1.jpg);
}
</style><link href="mytopic.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="table.inc.css">
</head>
<body>

<center>
 <table width="1000" border="0">
 <thead>
<tr>
    <td><img src="http://bank-network.info/images/logo.png" width="260" height="100" /></td>
  </tr>
<tr>
    <td><? include("menu.php"); ?></td>
  </tr>
  <tr>
    <td height="300"><img src="http://bank-network.info/images/flash.png" width="1000" height="300" /></td>
  </tr>
 
<tr>
    <td class="topic">สินค้าทั้งหมด</td>
</tr>   
</thead>

<form name="form1">
<input type="hidden" name="productid" />
    <input type="hidden" name="command" />
</form>

<div align="center">

<table id="for# " class="simply th">

<tr>
<td>
<form id="form3" name="form3" method="post" action="search.php">
<FONT COLOR=#000000>ค้นหาสินค้า </FONT><input name="txt_search" type="text" value="<?=$_POST['txt_search']?>" /> <input name="Search" type="submit" value="ค้นหา" />
</form>
</td>
</tr>


<!--<table border="0" cellpadding="2px" width="600px">-->
<?
$result=mysql_query("select Category from files where Category = 'Phone'");
while($row=mysql_fetch_assoc($result)){
?>
<tbody>
    <tr>
        <td><center><img src="images/<?=$row['FilesName']?>" width="200" height="220" /></center></td>
            <td>    <b><?=$row['Name']?></b><br />
            <?=$row['Detail']?><br />
<b>Product ID :</b><?=$row['FilesID']?><br />
                    <b>Price:</b><big style="color:green">
                    $<?=$row['Price']?></big><br /><br />
                    <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['FilesID']?>)" />
</td>
</tr>
        <? } ?>
</tbody>
    </table>
</div>
<tfoot>
  <tr>
    <center><td height="118"><img src="http://bank-network.info/images/footer.png" width="1000" height="114" /></td></center>
  </tr>
  </tfoot>
</table>
</center>
<script type="text/javascript">
function popup(url,name,windowWidth,windowHeight){   
myleft=(screen.width)?(screen.width-windowWidth)/2:100;
mytop=(screen.height)?(screen.height-windowHeight)/2:100;
properties = "width=" windowWidth ",height=" windowHeight;
properties  =",scrollbars=yes, top=" mytop ",left=" myleft;   
window.open(url,name,properties);
}
</script>
</body>
</html>


เอาข้อมูลทุก field มาแสดงก็ต้องใช้ select * from ครับ ที่ผมยกตัวอย่างเพราะเห็นคำถามว่าต้องการดึงแค่ฟิลด์ Category ลองให้ * ดูครับ ข้อมูลน่าจะมา  :'(


555 ขอบคุณมากครับทุกๆท่าน wanwan017  1
บันทึกการเข้า
หน้า: [1]   ขึ้นบน
พิมพ์