initial commit
This commit is contained in:
12
composer.yml
Normal file
12
composer.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "WPDB-Alternative",
|
||||
"description": "Gives you access to main $wpdb->functions",
|
||||
"version": "1.0",
|
||||
"type": "library",
|
||||
"authors": "vorbild",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"WP_DB\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
||||
129
src/wp_db.php
Normal file
129
src/wp_db.php
Normal file
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
class WP_DB
|
||||
{
|
||||
|
||||
public $host = ''; // oder die IP-Adresse des Datenbankservers
|
||||
public $db = ''; // Name deiner Datenbank
|
||||
public $user = ''; // Datenbankbenutzername
|
||||
public $pass = ''; // Passwort des Datenbankbenutzers
|
||||
public $charset = ''; // Zeichencodierung
|
||||
public $prefix = 'wp_';
|
||||
|
||||
function connect()
|
||||
{
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => 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);
|
||||
}
|
||||
}
|
||||
20
src/wp_db_light.php
Normal file
20
src/wp_db_light.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/wp_db.php');
|
||||
|
||||
class WP_DB_LIGHT extends WP_DB
|
||||
{
|
||||
public $dbName;
|
||||
|
||||
function connect()
|
||||
{
|
||||
$databaseFile = '/sql/'.$this->dbName.'.db';
|
||||
$pdo = new PDO("sqlite:$databaseFile");
|
||||
|
||||
// Set the error mode to exception
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user