dongxianghui3709
2014-06-29 00:51
浏览 349
已采纳

PDO MySQL SELECT在IF语句中具有多个条件

I am a newbie and wish to learn the following;

I wish to query MySQL with PHP (my PDO connection is $dbo);

I have a the variables;

  • $form_category //an integer
  • $form_subcategory //an integer
  • $form_subcategory2 //text
  • $fname //the file name
  • $form_class // 'create' is the class in this example
  • $form_location //directory where the pdf is stored

My attempt at the query is;

///start query ///

  $quer2 = $dbo->prepare("SELECT form_id,form_description
    FROM form_detail
    WHERE form_name < :form_name AND form_category = :form_category AND form_subcategory = :form_subcategory AND form_subcategory2 = :form_subcategory2");
        $quer2 ->bindParam(':form_id', $form_id);
    $quer2 ->bindParam(':form_name', $fname);
    $quer2 ->bindParam(':form_category', $catid);
    $quer2 ->bindParam(':form_subcategory', $subcatid);
    $quer2 ->bindParam(':form_subcategory2', $subcat2);
    $quer2 ->bindParam(':form_class', $form_class);
    $quer2 ->bindParam(':form_cerfa', $form_cerfa);
    $quer2 ->bindParam(':form_description', $form_description);
    $quer2 ->execute();

   ///End new query ///

I wish to output the 'form_reference' and 'form_description' from the variables above as well as the form name($fname) and display it in a table with href on the filename for example; echo "<td colspan=\"3\" rowspan=\"1\" style=\"text-align: center; vertical-align: middle;\"><a href=".$dir."create/".$fname." target=\"_blank\">".$fname."</a></td></tr>";

图片转代码服务由CSDN问答提供 功能建议

我是新手,希望学习以下内容;

我希望 使用PHP查询MySQL(我的PDO连接是$ dbo);

我有一个变量;

  • $ form_category / /一个整数
  • $ form_subcategory //一个整数
  • $ form_subcategory2 // text
  • $ fname //文件名
  • $ form_class //'create'是本例中的类
  • $ form_location //存储pdf的目录 \ n

    我对查询的尝试是;

      ///开始查询/// 
     
     $ quer2 = $ dbo-&gt; prepare(“  SELECT form_id,form_description 
     FROM form_detail 
     WHERE form_name&lt;:form_name AND form_category =:form_category AND form_subcategory =:form_subcategory AND form_subcategory2 =:form_subcategory2“); 
     $ quer2  - &gt; bindParam(':form_id',$ form_id  ); 
     $ quer2  - &gt; bindParam(':form_name',$ fname); 
     $ quer2  - &gt; bindParam(':form_category',$ catid); 
     $ quer2  - &gt; bindParam(':form_  subategory',$ subcatid); 
     $ quer2  - &gt; bindParam(':form_subcategory2',$ subcat2); 
     $ quer2  - &gt; bindParam(':form_class',$ form_class); 
     $ quer2  - &gt;  bindParam(':form_cerfa',$ form_cerfa); 
     $ quer2  - &gt; bindParam(':form_description',$ form_description); 
     $ quer2  - &gt; execute(); 
     
     ///结束新查询 /// 
       
     
     

    我希望从上面的变量输出'form_reference'和'form_description'以及表单名称($ fname)并显示在 例如,在文件名上使用href的表; echo“&lt; td colspan = \”3 \“rowspan = \”1 \“style = \”text-align:center; vertical-align:middle; \“&gt;&lt; a href =”。$ dir。“create /".$ fname。”target = \“_ blank \”&gt;“。$ fname。”&lt; / a&gt;&lt; ; / td&gt;&lt; / tr&gt;“;

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqin6785 2014-06-29 01:17
    已采纳

    Do it this way ,

    $quer2 ->execute();
    if ($sp->execute())  // Verify that result exist
    {   
    
            $result=$quer2->fetch(PDO::FETCH_OBJ);
            $result->closeCursor();
    
    //   Now , Access Form id and description in this way 
    
    
    
        echo "Form id : $result->form_id";
        echo "Form Description : $result->form_description" ; 
    }       
    

    Another thing ,

    $quer2 ->bindParam(':form_id', $form_id);

    If It's an int modify it like this , $quer2 ->bindParam(':form_id', $form_id , PDO::PARAM_INT);

    And if string then PDO::PARAM_STR .

    It's a good practice and gives you added security.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题