Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MySQLDB {
- var $connection; // The MySQL database connection
- /* Class constructor */
- function MySQLDB() {
- global $dbsystem;
- $this->connection = mysqli_connect ( DB_SERVER, DB_USER, DB_PASS, DB_NAME ) or die ( 'Connection Failed (' . mysqli_connect_errno () . ') ' . mysqli_connect_error () );
- }
- /**
- * query - Performs the given query on the database and
- * returns the result, which may be false, true or a
- * resource identifier.
- */
- function query($query) {
- return mysqli_query ( $this->connection, $query );
- }
- };
- /* Create database connection */
- $database = new MySQLDB ();
- $q = "UPDATE users SET name = '$name', started = '$time' WHERE id = '$id';";
- $result = mysqli_query ( $database->connection, $q );
- <?php
- class MySQLDB{
- private function openConnection(){
- // If you don't always use same credentials, pass them by params
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $database = "database";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $database);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // Assign conection object
- return $conn;
- }
- private function closeConnection($conn){
- $conn->close();
- }
- function updateUserById($id, $name, $startedTime){
- $conn = $this->openConnection();
- // Array of arrays to store the results
- // You can use any other method you want to return them
- $resultsArray = [];
- $sqlQuery = "UPDATE users SET name = ?, started = ? WHERE id = ?";
- if ($stmt = $conn->prepare($sqlQuery)) {
- // Bind parameters
- $stmt->bind_param("ssi", $name, $startedTime, $id);
- // Execute query
- $stmt->execute();
- if ($stmt->errno) {
- die ( "Update failed: " . $stmt->error);
- }
- $stmt->close();
- }
- $this->closeConnection($conn);
- }
- } // Class end
- <?php
- $myDBHandler = new MySQLDB();
- $myDBHandler->updateUserById(3, "Mark", 1234);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement