_pdo.php clean up and comments

This commit is contained in:
Hash Borgir 2023-06-01 23:29:56 -06:00
parent ec45a9c1b6
commit b56885e3fc

122
_pdo.php
View File

@ -1,22 +1,47 @@
<?php <?php
// PDO helper functions. /**
// Copyright (c) 2012-2014 The PHP Desktop authors. All rights reserved. * PDO helper functions.
// License: New BSD License. *
// Website: http://code.google.com/p/phpdesktop/ * 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
*/
function PDO_Connect($dsn, $user="", $password="") /**
* 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 = "")
{ {
global $PDO; global $PDO;
$PDO = new PDO($dsn, $user, $password); $PDO = new PDO($dsn, $user, $password);
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$PDO->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); $PDO->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
} }
function PDO_FetchOne($query, $params=null)
/**
* 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)
{ {
global $PDO; global $PDO;
if (isset($params)) { if (isset($params)) {
$stmt = $PDO->prepare($query,array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false)); $stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
$stmt->execute($params); $stmt->execute($params);
} else { } else {
$stmt = $PDO->query($query); $stmt = $PDO->query($query);
@ -25,78 +50,129 @@ function PDO_FetchOne($query, $params=null)
if ($row) { if ($row) {
return $row[0]; return $row[0];
} else { } else {
return false; return null;
} }
} }
function PDO_FetchRow($query, $params=null)
/**
* 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)
{ {
global $PDO; global $PDO;
if (isset($params)) { if (isset($params)) {
$stmt = $PDO->prepare($query,array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false)); $stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
$stmt->execute($params); $stmt->execute($params);
} else { } else {
$stmt = $PDO->query($query); $stmt = $PDO->query($query);
} }
return $stmt->fetch(PDO::FETCH_ASSOC); return $stmt->fetch(PDO::FETCH_ASSOC);
} }
function PDO_FetchAll($query, $params=null)
/**
* 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)
{ {
global $PDO; global $PDO;
if (isset($params)) { if (isset($params)) {
$stmt = $PDO->prepare($query,array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false)); $stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
$stmt->execute($params); $stmt->execute($params);
} else { } else {
$stmt = $PDO->query($query); $stmt = $PDO->query($query);
} }
return $stmt->fetchAll(PDO::FETCH_ASSOC); return $stmt->fetchAll(PDO::FETCH_ASSOC);
} }
function PDO_FetchAllAssoc($query, $params=null)
/**
* 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; global $PDO;
if (isset($params)) { if (isset($params)) {
$stmt = $PDO->prepare($query,array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false)); $stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
$stmt->execute($params); $stmt->execute($params);
} else { } else {
$stmt = $PDO->query($query); $stmt = $PDO->query($query);
} }
return $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP); return $stmt->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP);
} }
function PDO_FetchAssoc($query, $params=null)
/**
* 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)
{ {
global $PDO; global $PDO;
if (isset($params)) { if (isset($params)) {
$stmt = $PDO->prepare($query,array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false)); $stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
$stmt->execute($params); $stmt->execute($params);
} else { } else {
$stmt = $PDO->query($query); $stmt = $PDO->query($query);
} }
$rows = $stmt->fetchAll(PDO::FETCH_NUM); $rows = $stmt->fetchAll(PDO::FETCH_NUM);
$assoc = array(); $assoc = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$assoc[$row[0]] = $row[1]; $assoc[$row[0]] = $row[1];
} }
return $assoc; return $assoc;
} }
function PDO_Execute($query, $params=null)
/**
* 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)
{ {
global $PDO; global $PDO;
if (isset($params)) { if (isset($params)) {
$stmt = $PDO->prepare($query,array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false)); $stmt = $PDO->prepare($query, [PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false]);
$stmt->execute($params); $stmt->execute($params);
return $stmt; return $stmt;
} else { } else {
return $PDO->query($query); return $PDO->query($query);
} }
} }
/**
* Returns the last inserted row ID.
*
* @return string The last inserted row ID.
*/
function PDO_LastInsertId() function PDO_LastInsertId()
{ {
global $PDO; global $PDO;
return $PDO->lastInsertId(); return $PDO->lastInsertId();
} }
/**
* Returns information about the last error that occurred on the database connection.
*
* @return array The error information as an array.
*/
function PDO_ErrorInfo() function PDO_ErrorInfo()
{ {
global $PDO; global $PDO;
return $PDO->errorInfo(); return $PDO->errorInfo();
} }
?> ?>