×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: userc865b
Added: Aug 7, 2017 1:45 PM
Modified: Aug 7, 2017 3:06 PM
Views: 7
Tags: php
  1. <?    
  2. // bind Parameters Elegant Way:
  3. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // sorgt dafür, dass eine Exception geworfen wird!
  4.  
  5. $whereArtikel="test1";
  6. $whereArtikel2="test2";
  7. $sql="SELECT Surname FROM Contacts WHERE Surname = :art OR Surname = :bin";
  8. //$sql2="SELECT Surname FROM Contacts WHERE Surname = ? OR Surname = ?";
  9.     if($stmt=$conn->prepare($sql)){
  10.         if ($stmt->execute([':art'=>$whereArtikel,':bin'=>$whereArtikel2])){//execute(['test1','test2'])
  11.                 $stmt->rowCount();
  12.             while ($row=$stmt->fetch()){ // OR $row=$stmt->fetch(PDO::FETCH_OBJ) --> returns Object | PDO::FETCH_NUM -> indiziertes Array
  13.                 echo $row['Surname']."<br/>"; // $row->Surname
  14.             }
  15.         }
  16.  
  17. }
  18.  
  19. // Bind Parameters with bindValue
  20.         $whereArtikel='483-00-00';
  21.  
  22.     $sql="SELECT ArtikelNr FROM _IQF2_FK_Artikel WHERE ArtikelNr = :art";
  23.     if($stmt=$conn->prepare($sql)){
  24.         $stmt->bindValue(':art',$whereArtikel);
  25.         if ($stmt->execute()){
  26.                 $stmt->rowCount();
  27.             while ($row=$stmt->fetch()){
  28.                 echo $row['ArtikelNr']."<br/>";
  29.             }
  30.         }
  31.  
  32.     }
  33. // Bind Parameters with bindParam
  34. // https://stackoverflow.com/questions/12392424/pdo-bindparam-vs-execute
  35.  
  36. ################ EXAMPLE########################:
  37.         $whereArtikel='483-00-00';
  38.  
  39.     $sql="SELECT ArtikelNr FROM _IQF2_FK_Artikel WHERE ArtikelNr = :art";
  40.     if($stmt=$conn->prepare($sql)){
  41.         $stmt->bindParam(':art',$whereArtikel);
  42.         if ($stmt->execute()){
  43.                 $stmt->rowCount();
  44.             while ($row=$stmt->fetch()){
  45.                 echo $row['ArtikelNr']."<br/>";
  46.             }
  47.         }
  48.  
  49.     }
  50.  
  51.  
  52. ################DIFFERENCE BETWEEN bindValue and BindParam##############################
  53. // erster Unterschied: bindValue kann man sowohl Variablen als auch Strings als Parameter angeben
  54. // bind_param nur Variablen!!
  55.  
  56. $someVal=10;
  57. $someVal2=20;
  58. /* In bindParam, the value argument is not bound and
  59. will be changed if we change its value before execute.
  60. */
  61. $ref->bindParam(':someCol',$someVal);
  62. $someVal=$someVal2;
  63. $ref->execute();
  64. //someCol=20
  65. /* In bindValue, the value argument is bound and
  66. never changed if we change its value before execute.
  67. */
  68. $ref->bindValue(':someCol',$someVal);
  69. // here assignment is referral (&$someVal)
  70. $someVal=$someVal2;
  71. $ref->execute();
  72. //someCol=10
  73.