>> สอบถาม SQL การ select ข้อมูล <<<

เริ่มโดย bankker6480, 19 กุมภาพันธ์ 2013, 17:59:50

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

bankker6480

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

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

thatthep


gubaaball

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



อย่างนี้ป่ะครับ ผิดพลาดยังไงก็ขออภัยด้วย  :'(

p44n

การเก็บข้อมูล แบบ 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

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

p44n

อ้างถึงจาก: bankker6480 ใน 19 กุมภาพันธ์ 2013, 18:31:47
ดึงออกมาเป็นแบบนี้อ่าครับ
คือจำนวนถูก แต่ดึงข้อมูลออกมาไม่ได้
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

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

อ้างถึงจาก: bankker6480 ใน 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>


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

bankker6480

อ้างถึงจาก: gubaaball ใน 19 กุมภาพันธ์ 2013, 18:41:27
อ้างถึงจาก: bankker6480 ใน 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>


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

555 ขอบคุณมากครับทุกๆท่าน :wanwan017:  1