简体   繁体   中英

PDO MySQL SELECT with multiple criteria within an IF statement

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM