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.

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

报告相同问题?