From b9c06a1ec614d4dd3a72b2cba99fc793e380fba6 Mon Sep 17 00:00:00 2001 From: vorbildunternehmer Date: Wed, 4 Dec 2024 08:28:57 +0100 Subject: [PATCH] initial commit --- composer.json | 16 ++++++ src/wp_db.php | 129 ++++++++++++++++++++++++++++++++++++++++++++ src/wp_db_light.php | 20 +++++++ 3 files changed, 165 insertions(+) create mode 100644 composer.json create mode 100644 src/wp_db.php create mode 100644 src/wp_db_light.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..06ac12a --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "vorbild/wpdb-alternative", + "description": "Gives you access to main $wpdb->functions", + "version": "1.0", + "type": "library", + "authors": [ + { + "name": "vorbild" + } + ], + "autoload": { + "psr-4": { + "WP_DB\\": "src/" + } + } +} \ No newline at end of file diff --git a/src/wp_db.php b/src/wp_db.php new file mode 100644 index 0000000..43f3215 --- /dev/null +++ b/src/wp_db.php @@ -0,0 +1,129 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_EMULATE_PREPARES => false, + ]; + + $dsn = "mysql:host=$this->host;dbname=$this->db;charset=utf8"; + + try { + $pdo = new PDO($dsn, $this->user, $this->pass, $options); + } catch (\PDOException $e) { + throw new \PDOException($e->getMessage(), (int)$e->getCode()); + } + return $pdo; + } + + function get_results($sql, $atts = []) + { + + $pdo = $this->connect(); + $stmt = $pdo->prepare($sql); + $stmt->execute($atts); + + $results = $stmt->fetchAll(); + return $results; + } + + function get_row($sql, $atts = []) + { + + $pdo = $this->connect(); + $stmt = $pdo->prepare($sql); + $stmt->execute($atts); + + $result = $stmt->fetch(); + return $result; + } + + function get_var($sql, $atts = []) + { + + $pdo = $this->connect(); + $stmt = $pdo->prepare($sql); + $stmt->execute($atts); + + $value = $stmt->fetchColumn(); + return $value; + } + + public function insert($table, $data) + { + $pdo = $this->connect(); // Ensure connection is established + + // Prepare the SQL statement + $columns = implode(", ", array_keys($data)); + $placeholders = ":" . implode(", :", array_keys($data)); + $sql = "INSERT INTO $table ($columns) VALUES ($placeholders)"; + + $stmt = $pdo->prepare($sql); + + // Bind parameters + foreach ($data as $key => &$value) { + $stmt->bindParam(":$key", $value); + } + + // Execute the statement + return $stmt->execute(); + } + + public function update($table, $data, $where) + { + $pdo = $this->connect(); // Ensure connection is established + + // Prepare the SQL statement + $setParts = []; + foreach ($data as $key => $value) { + $setParts[] = "$key = :$key"; + } + $setString = implode(", ", $setParts); + + // Prepare where clause + $whereParts = []; + foreach ($where as $key => $value) { + $whereParts[] = "$key = :where_$key"; + } + $whereString = implode(" AND ", $whereParts); + + $sql = "UPDATE $table SET $setString WHERE $whereString"; + + $stmt = $pdo->prepare($sql); + + // Bind parameters for the set values + foreach ($data as $key => &$value) { + $stmt->bindParam(":$key", $value); + } + + // Bind parameters for the where values + foreach ($where as $key => &$value) { + $stmt->bindParam(":where_$key", $value); + } + + // Execute the statement + return $stmt->execute(); + } + + public function query($sql, $params = []) + { + $pdo = $this->connect(); // Ensure connection is established + + $stmt = $pdo->prepare($sql); + + // Execute the statement with parameters if provided + return $stmt->execute($params); + } +} diff --git a/src/wp_db_light.php b/src/wp_db_light.php new file mode 100644 index 0000000..18b0836 --- /dev/null +++ b/src/wp_db_light.php @@ -0,0 +1,20 @@ +dbName.'.db'; + $pdo = new PDO("sqlite:$databaseFile"); + + // Set the error mode to exception + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + return $pdo; + } + + +}