Guest User

Untitled

a guest
Nov 28th, 2017
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $dbname = "myDBPDO";
  6.  
  7. try {
  8. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  9. // set the PDO error mode to exception
  10. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  12. VALUES ('John', 'Doe', 'john@example.com')";
  13. // use exec() because no results are returned
  14. $conn->exec($sql);
  15. echo "New record created successfully";
  16. }
  17. catch(PDOException $e) {
  18. echo $sql . "<br>" . $e->getMessage();
  19. }
  20.  
  21. $conn = null;
  22. ?>
  23.  
  24. <?php
  25. $servername = "localhost";
  26. $username = "root";
  27. $password = "";
  28. $dbname = "myDBPDO";
  29.  
  30. try {
  31. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  32. // set the PDO error mode to exception
  33. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  34. $last_id = $conn->lastInsertId();
  35. echo "Last inserted ID is: " . $last_id;
  36. }
  37. catch(PDOException $e) {
  38. echo $sql . "<br>" . $e->getMessage();
  39. }
  40.  
  41. $conn = null;
  42. ?>
  43.  
  44. Last inserted ID is: 0
  45.  
  46. Last inserted ID is: 1
Add Comment
Please, Sign In to add comment