Advertisement
Guest User

winta paula

a guest
Apr 11th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>PHP Data Objects (PDO) and MySQL</title>
  5. </head>
  6. <body>
  7.     <!-- used table from database ithtl:
  8.     student: { firstname: string, lastname: string }
  9.     -->
  10.     <?php
  11.         /* build database connection */
  12.         $dsn = 'mysql:dbname=ithtl;host127.0.0.1';
  13.         $user = 'root';
  14.         $password = '';
  15.        
  16.         try{
  17.            
  18.             $dbh = new PDO($dsn, $user, $password);
  19.            
  20.             echo "<h3>Simple query by using the statement query</h3>";
  21.            
  22.             $sql = "SELECT * FROM student";
  23.             $users = $dbh->query($sql);
  24.  
  25.             foreach($users as $row){
  26.                 print $row["firstname"] . " - " . $row["lastname"] . "<br>";
  27.             }
  28.            
  29.             echo "<h3>Prepared Statements with unnamed parameters</h3>";
  30.            
  31.             $statement = $dbh->prepare("SELECT * FROM student WHERE firstname = ? AND lastname = ?");
  32.             $statement->execute(array('Mas', 'Mustermann'));
  33.            
  34.             while($row = $statment->fetch()){
  35.                 echo $row["firstname"] . "   " . $row["lastname"] . "<br>";
  36.             }
  37.  
  38.         }catch(PDOException $e){
  39.             'Connection failed: ' . $e->getMessage();
  40.         }      
  41.     ?>
  42. </body>
  43. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement