Guest User

Untitled

a guest
Nov 26th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. bash# cat PHPpdoClient.php
  2.  
  3. <?php
  4.  
  5. $dsn='mysql:host=localhost;port=3306;dbname=test';
  6. $user='root';
  7. $pass='admin';
  8.  
  9. try {
  10.  
  11. /* obtain a database connection handle */
  12. $dbh = new PDO($dsn, $user, $pass);
  13.  
  14. } catch (PDOException $exception) {
  15. printf("Failed to connect to the database. Error: %s", $exception->getMessage());
  16. }
  17.  
  18. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19. $dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, false);
  20.  
  21. $sql = "SELECT * FROM City";
  22.  
  23. /* execute the query */
  24. $rs = $dbh->query($sql);
  25.  
  26. print "\nRetrieved " . $rs->rowCount() . " row(s).\n";
  27. print "\nCityName\n--------\n";
  28.  
  29. /* retrieve the data from the result set */
  30. $rs->setFetchMode(PDO::FETCH_ASSOC);
  31.  
  32. while ($row = $rs->fetch()) {
  33. $data = $row["CityName"];
  34. print "$data\n";
  35. }
  36.  
  37. /* close the result set */
  38. $rs->closeCursor();
  39.  
  40. try {
  41.  
  42. /* create a prepared statement */
  43. $query = "INSERT INTO City VALUES (?)";
  44. $stmt = $dbh->prepare($query);
  45.  
  46. $city = 'London, UK';
  47.  
  48. /* bind the parameter */
  49. $stmt->bindParam(1, $city);
  50.  
  51. $dbh->beginTransaction();
  52.  
  53. /* execute the SQL */
  54. if ($stmt->execute()) {
  55. $dbh->commit();
  56. echo "\nSuccessfuly inserted " . $city . " into the table, City.";
  57. } else {
  58. print_r($dbh->errorInfo());
  59. }
  60.  
  61. $city = 'Paris, France';
  62.  
  63. /* bind the parameter with another value */
  64. $stmt->bindParam(1, $city);
  65.  
  66. $dbh->beginTransaction();
  67.  
  68. /* execute the SQL again */
  69. if ($stmt->execute()) {
  70. $dbh->commit();
  71. echo "\nSuccessfuly inserted " . $city . " into the table, City.\n";
  72. } else {
  73. print_r($dbh->errorInfo());
  74. }
  75.  
  76. $sql = "SELECT * FROM City";
  77.  
  78. /* execute the query */
  79. $rs = $dbh->query($sql);
  80.  
  81. print "\nRetrieved " . $rs->rowCount() . " row(s).\n";
  82. print "\nCityName\n--------\n";
  83.  
  84. /* retrieve the data from the result set */
  85. $rs->setFetchMode(PDO::FETCH_OBJ);
  86.  
  87. while ($row = $rs->fetch()) {
  88. $data = $row->CityName;
  89. print "$data\n";
  90. }
  91.  
  92. /* close the result set */
  93. $rs->closeCursor();
  94.  
  95. } catch (PDOException $exception) {
  96. print "\nException: " . $exception->getMessage();
  97. $dbh->rollBack();
  98. }
  99.  
  100. /* close the database connection */
  101. $dbh = null;
  102.  
  103. ?>
  104.  
  105. bash# php PHPpdoClient.php
  106.  
  107. Retrieved 3 row(s).
  108.  
  109. CityName
  110. --------
  111. Hyderabad, India
  112. San Francisco, USA
  113. Sydney, Australia
  114.  
  115. Successfuly inserted London, UK into the table, City.
  116. Successfuly inserted Paris, France into the table, City.
  117.  
  118. Retrieved 5 row(s).
  119.  
  120. CityName
  121. --------
  122. Hyderabad, India
  123. San Francisco, USA
  124. Sydney, Australia
  125. London, UK
  126. Paris, France
Add Comment
Please, Sign In to add comment