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

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>";

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测