d2tools/_pdo.php

179 lines
5.0 KiB
PHP
Raw Normal View History

2021-03-24 10:26:51 +00:00
<?php
2023-06-02 05:29:56 +00:00
/**
* PDO helper functions.
*
* This code provides helper functions for working with PDO.
* It includes functions for connecting to a database, executing queries,
* fetching rows and values, and retrieving error information.
*
* @package PDO_Helper
* @copyright Copyright (c) 2012-2014 The PHP Desktop authors
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @link http://code.google.com/p/phpdesktop/ Website
*
* Commented and cleaned by Hash Borgir
*/
2021-03-24 10:26:51 +00:00
2023-06-02 05:29:56 +00:00
/**
* Connects to the database using PDO.
*
* @param string $dsn The Data Source Name, a string that describes the database source.
* @param string $user The username for the database connection.
* @param string $password The password for the database connection.
* @return void
*/
function PDO_Connect($dsn, $user = "", $password = "")
2021-03-24 10:26:51 +00:00
{
global $PDO;
$PDO = new PDO($dsn, $user, $password);
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
2023-06-02 05:29:56 +00:00
$PDO->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
2021-03-24 10:26:51 +00:00
}
2023-06-02 05:29:56 +00:00
/**
* Fetches a single value from a query.
*
* @param string $query The SQL query to execute.
* @param array|null $params An array of values to bind to the query parameters.
* @return mixed|null The fetched value, or null if no value is found.
*/
function PDO_FetchOne($query, $params = null)
2021-03-24 10:26:51 +00:00
{
global $PDO;
if (isset($params)) {
2023-06-02 05:29:56 +00:00
$stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
2021-03-24 10:26:51 +00:00
$stmt->execute($params);
} else {
$stmt = $PDO->query($query);
}
$row = $stmt->fetch(PDO::FETCH_NUM);
if ($row) {
return $row[0];
} else {
2023-06-02 05:29:56 +00:00
return null;
2021-03-24 10:26:51 +00:00
}
}
2023-06-02 05:29:56 +00:00
/**
* Fetches a single row from a query.
*
* @param string $query The SQL query to execute.
* @param array|null $params An array of values to bind to the query parameters.
* @return array|false The fetched row as an associative array, or false if no row is found.
*/
function PDO_FetchRow($query, $params = null)
2021-03-24 10:26:51 +00:00
{
global $PDO;
if (isset($params)) {
2023-06-02 05:29:56 +00:00
$stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
2021-03-24 10:26:51 +00:00
$stmt->execute($params);
} else {
$stmt = $PDO->query($query);
}
return $stmt->fetch(PDO::FETCH_ASSOC);
}
2023-06-02 05:29:56 +00:00
/**
* Fetches all rows from a query.
*
* @param string $query The SQL query to execute.
* @param array|null $params An array of values to bind to the query parameters.
* @return array The fetched rows as an associative array.
*/
function PDO_FetchAll($query, $params = null)
2021-03-24 10:26:51 +00:00
{
global $PDO;
if (isset($params)) {
2023-06-02 05:29:56 +00:00
$stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
2021-03-24 10:26:51 +00:00
$stmt->execute($params);
} else {
$stmt = $PDO->query($query);
}
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
2023-06-02 05:29:56 +00:00
/**
* Fetches all rows from a query and groups them by a specific column.
*
* @param string $query The SQL query to execute.
* @param array|null $params An array of values to bind to the query parameters.
* @return array The fetched rows as an associative array, grouped by the specified column.
*/
function PDO_FetchAllAssoc($query, $params = null)
{
global $PDO;
if (isset($params)) {
2023-06-02 05:29:56 +00:00
$stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
$stmt->execute($params);
} else {
$stmt = $PDO->query($query);
}
2023-06-02 05:29:56 +00:00
return $stmt->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP);
}
2023-06-02 05:29:56 +00:00
/**
* Fetches key-value pairs from a query.
*
* @param string $query The SQL query to execute.
* @param array|null $params An array of values to bind to the query parameters.
* @return array The fetched key-value pairs as an associative array.
*/
function PDO_FetchAssoc($query, $params = null)
2021-03-24 10:26:51 +00:00
{
global $PDO;
if (isset($params)) {
2023-06-02 05:29:56 +00:00
$stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
2021-03-24 10:26:51 +00:00
$stmt->execute($params);
} else {
$stmt = $PDO->query($query);
}
$rows = $stmt->fetchAll(PDO::FETCH_NUM);
2023-06-02 05:29:56 +00:00
$assoc = [];
2021-03-24 10:26:51 +00:00
foreach ($rows as $row) {
$assoc[$row[0]] = $row[1];
}
return $assoc;
}
2023-06-02 05:29:56 +00:00
/**
* Executes a query.
*
* @param string $query The SQL query to execute.
* @param array|null $params An array of values to bind to the query parameters.
* @return PDOStatement|false The PDOStatement object representing the executed query, or false on failure.
*/
function PDO_Execute($query, $params = null)
2021-03-24 10:26:51 +00:00
{
global $PDO;
if (isset($params)) {
2023-06-02 05:29:56 +00:00
$stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
2021-03-24 10:26:51 +00:00
$stmt->execute($params);
return $stmt;
} else {
return $PDO->query($query);
}
}
2023-06-02 05:29:56 +00:00
/**
* Returns the last inserted row ID.
*
* @return string The last inserted row ID.
*/
2021-03-24 10:26:51 +00:00
function PDO_LastInsertId()
{
global $PDO;
return $PDO->lastInsertId();
}
2023-06-02 05:29:56 +00:00
/**
* Returns information about the last error that occurred on the database connection.
*
* @return array The error information as an array.
*/
2021-03-24 10:26:51 +00:00
function PDO_ErrorInfo()
{
global $PDO;
return $PDO->errorInfo();
}
?>