Advertisement
Guest User

Untitled

a guest
Sep 10th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. class MySQLDB {
  2. var $connection; // The MySQL database connection
  3.  
  4. /* Class constructor */
  5. function MySQLDB() {
  6. global $dbsystem;
  7. $this->connection = mysqli_connect ( DB_SERVER, DB_USER, DB_PASS, DB_NAME ) or die ( 'Connection Failed (' . mysqli_connect_errno () . ') ' . mysqli_connect_error () );
  8. }
  9.  
  10.  
  11. /**
  12. * query - Performs the given query on the database and
  13. * returns the result, which may be false, true or a
  14. * resource identifier.
  15. */
  16. function query($query) {
  17. return mysqli_query ( $this->connection, $query );
  18. }
  19. };
  20.  
  21. /* Create database connection */
  22. $database = new MySQLDB ();
  23.  
  24. $q = "UPDATE users SET name = '$name', started = '$time' WHERE id = '$id';";
  25. $result = mysqli_query ( $database->connection, $q );
  26.  
  27. <?php
  28.  
  29. class MySQLDB{
  30.  
  31. private function openConnection(){
  32.  
  33. // If you don't always use same credentials, pass them by params
  34. $servername = "localhost";
  35. $username = "username";
  36. $password = "password";
  37. $database = "database";
  38.  
  39. // Create connection
  40. $conn = new mysqli($servername, $username, $password, $database);
  41.  
  42. // Check connection
  43. if ($conn->connect_error) {
  44. die("Connection failed: " . $conn->connect_error);
  45. }
  46.  
  47. // Assign conection object
  48. return $conn;
  49. }
  50.  
  51. private function closeConnection($conn){
  52. $conn->close();
  53. }
  54.  
  55. function updateUserById($id, $name, $startedTime){
  56.  
  57. $conn = $this->openConnection();
  58.  
  59. // Array of arrays to store the results
  60. // You can use any other method you want to return them
  61. $resultsArray = [];
  62.  
  63. $sqlQuery = "UPDATE users SET name = ?, started = ? WHERE id = ?";
  64.  
  65. if ($stmt = $conn->prepare($sqlQuery)) {
  66.  
  67. // Bind parameters
  68. $stmt->bind_param("ssi", $name, $startedTime, $id);
  69.  
  70. // Execute query
  71. $stmt->execute();
  72.  
  73. if ($stmt->errno) {
  74. die ( "Update failed: " . $stmt->error);
  75. }
  76.  
  77. $stmt->close();
  78. }
  79.  
  80. $this->closeConnection($conn);
  81. }
  82.  
  83. } // Class end
  84.  
  85. <?php
  86.  
  87. $myDBHandler = new MySQLDB();
  88.  
  89. $myDBHandler->updateUserById(3, "Mark", 1234);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement