|  | 
	                
  
    | 
	 
        เขียน php อย่างไรเพื่อให้ค่าตัวแปรมาต่อกันในรูปแบบของคำสั่ง sql     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | สอบถามครับจากตัวอย่างด้านล่างเป็น code ของ VB ผมต้องการได้ค่าตัวแปร sSql เพื่อนำไปใช้ select ในฐานข้อมูล แต่ติดเรื่องเขียนอย่างไรถึงได้ค่า sSql ออกมา กรณีนำค่าตัวแปรมาต่อกันครับ โดยใช้ภาษา PHP มือใหม่ยังเขียนไม่คล่องครับ
 รบกวนด้วยครับ (ตรง select ของ sql ถูกผิดไม่เป็นไรที่สำคัญคือ วิธีเขียนให้ค่า sSql มาต่อกันแค่นั้นครับ)
 
 Code
 
 Private Sub INT_Data()
        Dim rs As ADODB.Recordset
        Dim sSql As String, sEmp As String
        On Error GoTo msgErr:
        
        If cboCust_type.Text <> "" Then
                sSql = "SELECT DISTINCT b.SEQ, f.CUST_NAME,b.REF_NO, g.GOODS_ID, g.QTY,b.AGENT_NAME, f.TEL2, f.AMPHUR1,b.CUST_TYPE, " & _
                                " e.EMP_NAME,b.time_confirm,b.in_time,b.out_time,b.staff_in,b.job_num,b.output,b.descr,b.problem,b.sale_inform,a.pl_no,nvl(b.amount,0)amount,to_char(a.pl_date,'dd/mm/yyyy')pl_date " & _
                        " FROM   SER_PLAN_MASTER a inner join SER_PLAN_DETAIL b On a.pl_no=b.pl_no " & _
                                " left outer join SER_JOB c On b.ref_no = c.job_no " & _
                                " left outer join SER_PLAN_EMP d On b.pl_no=d.pl_no " & _
                                " left outer join SER_EMPLOYEE e On d.emp_id = e.emp_id " & _
                                " left outer join SER_CUSTOMER f On c.cust_no = f.cust_no " & _
                                " left outer join SER_JOB_DETAIL g On c.job_no = g.job_no " & _
                        " Where nvl(a.pl_status,'N')='N' and a.pl_date between to_date('" & Left(dtpDate1, 6) & Year(dtpDate1) & "','dd/mm/yyyy') and to_date('" & Left(dtpDate2, 6) & Year(dtpDate2) & "','dd/mm/yyyy')" & _
                                " and b.cust_type = '" & cboCust_type.Text & "'"
        Else            'All
                sSql = "SELECT DISTINCT b.SEQ, f.CUST_NAME,b.REF_NO, g.GOODS_ID, g.QTY,b.AGENT_NAME, f.TEL2, f.AMPHUR1,b.CUST_TYPE, " & _
                                " e.EMP_NAME,b.time_confirm,b.in_time,b.out_time,b.staff_in,b.job_num,b.output,b.descr,b.problem,b.sale_inform,a.pl_no,nvl(b.amount,0)amount,to_char(a.pl_date,'dd/mm/yyyy')pl_date " & _
                        " FROM   SER_PLAN_MASTER a inner join SER_PLAN_DETAIL b On a.pl_no=b.pl_no " & _
                                " left outer join SER_JOB c On b.ref_no = c.job_no " & _
                                " left outer join SER_PLAN_EMP d On b.pl_no=d.pl_no " & _
                                " left outer join SER_EMPLOYEE e On d.emp_id = e.emp_id " & _
                                " left outer join SER_CUSTOMER f On c.cust_no = f.cust_no " & _
                                " left outer join SER_JOB_DETAIL g On c.job_no = g.job_no " & _
                        " Where nvl(a.pl_status,'N')='N' and a.pl_date between to_date('" & Left(dtpDate1, 6) & Year(dtpDate1) & "','dd/mm/yyyy') and to_date('" & Left(dtpDate2, 6) & Year(dtpDate2) & "','dd/mm/yyyy')"
        End If
        If cboResult.Text <> "" Then
                If cboResult.Text = "จบ" Then sSql = sSql & " and b.output in ('สั่งผลิต','เสนอราคา')"
                If cboResult.Text = "ไม่จบ" Then sSql = sSql & " and nvl(b.output,'ไม่จบ') in ('ไม่จบ','เข้าอีก')"
        End If
        If Left(sSiteCode, 1) = "b" Then
                sSql = sSql & " And a.pl_type = '" & Cbo_TrimString(cboPL_type) & "'"
        End If
        sSql = sSql & " And a.site_code = '" & Left(sSiteCode, 1) & "'"
        sSql = sSql & " ORDER BY a.pl_no,b.seq"
        'Debug.Print sSql ให้ได้ค่า sSql แบบนำมาต่อกันในรูปแบบของ PHP ครับ
 
 ถ้าต้องการเขียน sSql ออกมาในรูปแบบของ PHP ต้องเขียนอย่างไรครับติดปัญหาตรงจะเขียนอย่างไรให้ sSql มาต่อกันได้ เพื่อนำ ตัวแปร sSql ไปใช้ select ในฐานข้อมูลต่อครับยังงงๆ
 อยู่ลองแล้วยังไม่ได้ครับ รบกวนอาจารย์ผู้รู้ทุกๆ ท่านช่วยแนะนำหน่อยครับ
 ขอบคุณมากครับ
 
 
 
 Tag : PHP
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2014-12-18 09:16:49 | By :
                          puupha | View :
                          975 | Reply :
                          5 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | PHP ทำได้หลายวิธีมากครับ จะเขียนต่อกันเลยก็ได้ครับ เช่น 
 Code
 
 $strSQL = "
SELECT DISTINCT b.SEQ, f.CUST_NAME,b.REF_NO, g.GOODS_ID, g.QTY,b.AGENT_NAME, f.TEL2, f.AMPHUR1,b.CUST_TYPE, 
e.EMP_NAME,b.time_confirm,b.in_time,b.out_time,b.staff_in,b.job_num,b.output,b.descr,b.problem,b.sale_inform,a.pl_no,nvl(b.amount,0)amount,to_char(a.pl_date,'dd/mm/yyyy')pl_date
                        FROM   SER_PLAN_MASTER a inner join SER_PLAN_DETAIL b On a.pl_no=b.pl_no
                                left outer join SER_JOB c On b.ref_no = c.job_no
";
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-12-18 10:01:57 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2014-12-18 10:01:57 
 รายละเอียดของการตอบ ::
 ขอบคุณคุณวินที่ช่วยแนะนำ แต่มันยังไม่ตรงจุดประสงค์ครับ 
ด้านล่างนี้ผมเขียนเป็น php แล้วแต่ติดปัญหาตรงข้อ 1 ที่จะนำค่าในตัวแปร $strSql มาต่อกันครับ 
ผมจำเป็นต้องเขียนในลักษณะนี้ ไม่ทราบพอจะแนะนำได้ไหมครับ ผมเขียนแล้วยัง Error อยู่ครับ
Code  if($_GET["listType"]=="ORC"  && $_GET["custType"]!="All"){
	 echo "case1";
		       $strSQL = "SELECT DISTINCT b.SEQ,f.CUST_NAME,b.REF_NO,b.evl_no,g.GOODS_ID, g.QTY,b.fg_status, f.TEL2, f.AMPHUR1,b.CUST_TYPE,i.cust_name cust_name1, a.car_driver,b.time_confirm,b.in_time,b.out_time,b.job_num,b.output,nvl(d.grand_total,0)evl_amount,b.descr,b.problem,b.fixed_detail,a.pl_no,nvl(b.amount,0)amount ,i.tel2 tel,i.amphur1 amphur,h.goods_id pm_fg,nvl(c.warranty,'-')warranty,c.cust_no,h.cust_no cust_no_pm,nvl(g.sc_no,'-')sc_no,to_char(a.pl_date,'dd/mm/yyyy')pl_date FROM   SER_PLAN_MASTER a
			    inner join SER_PLAN_DETAIL b on a.pl_no=b.pl_no
				left outer join SER_JOB c on b.ref_no=c.job_no 
				left outer join SER_EVALUATION_MASTER d on b.evl_no=d.evl_no 
				left outer join SER_CUSTOMER f on c.Cust_No=f.cust_no
				left join SER_JOB_DETAIL g on c.job_no=g.job_no 
				left outer join SER_PM_MASTER h on b.ref_no=h.pm_no
				left outer join SER_CUSTOMER i on h.cust_no=i.cust_no 
				Where nvl(a.pl_status,'N')='N' and a.pl_type='".$_GET["listType"]."' and to_char(a.pl_date,'yyyy/mm/dd')between '".$_GET["dateStart"]."' and '".$_GET["dateEnd"]."' and b.cust_Type='".$_GET["custType"]."' ORDER BY a.pl_no  ASC ";
 }else {
	  echo "case2";		   
		   $strSQL = "SELECT DISTINCT b.SEQ,f.CUST_NAME,b.REF_NO,b.evl_no,g.GOODS_ID, g.QTY,b.fg_status, f.TEL2, f.AMPHUR1,b.CUST_TYPE,i.cust_name cust_name1, a.car_driver,b.time_confirm,b.in_time,b.out_time,b.job_num,b.output,nvl(d.grand_total,0)evl_amount,b.descr,b.problem,b.fixed_detail,a.pl_no,nvl(b.amount,0)amount ,i.tel2 tel,i.amphur1 amphur,h.goods_id pm_fg,nvl(c.warranty,'-')warranty,c.cust_no,h.cust_no cust_no_pm,nvl(g.sc_no,'-')sc_no,to_char(a.pl_date,'dd/mm/yyyy')pl_date FROM   SER_PLAN_MASTER a inner join SER_PLAN_DETAIL b on a.pl_no=b.pl_no left outer join SER_JOB c on b.ref_no=c.job_no left outer join SER_EVALUATION_MASTER d on b.evl_no=d.evl_no left outer join SER_CUSTOMER f on c.Cust_No=f.cust_no left join SER_JOB_DETAIL g on c.job_no=g.job_no left outer join SER_PM_MASTER h on b.ref_no=h.pm_no left outer join SER_CUSTOMER i on h.cust_no=i.cust_no Where nvl(a.pl_status,'N')='N' and to_char(a.pl_date,'yyyy/mm/dd')between '".$_GET["dateStart"]."' and '".$_GET["dateEnd"]."' ORDER BY a.pl_no  ASC ";	  
}
///If cboResult.Text <> "" Then
   ///             If cboResult.Text = "จบ" Then sSql = sSql & " and b.output in ('สั่งผลิต','เสนอราคา')"
      ///          If cboResult.Text = "ไม่จบ" Then sSql = sSql & " and nvl(b.output,'ไม่จบ') in ('ไม่จบ','เข้าอีก')"
       /// End If
// 1.ปัญหาจะนำ $strSQL ด้านล่างที่อยู่ในเงื่อนไขไปต่อกับ $strSQL ด้านบนครับ   แต่มันยัง error จะเขียนโค้ดอย่างไรครับ
If($_GET["jobStatus"]!="All" {
                If($_GET["jobStatus"]=="จบ" { $strSQL = $strSQL & " and b.output in ('สั่งผลิต','เสนอราคา')";{
                If($_GET["jobStatus"]=="ไม่จบ" { $strSQL = $strSQL & " and nvl(b.output,'ไม่จบ') in ('ไม่จบ','เข้าอีก')";{					
}
 
ขอบคุณอย่างมากครับ
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-12-18 14:26:18 | By :
                            puupha |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | แบบนี้หรือเปล่า? Code (PHP)
 
 $sql = "INSERT INTO tbl ";
$sql .= "VALUES ( ";
$sql .= " '1111','2222' ";
$sql .= " ) ";
$obj_query = mysql_query($sql);
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2014-12-19 08:38:00 | By :
                            arm8957 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 |  |