Place Holder Projects Code
Bash MySQL JavaScript MySQL Quick Reference
Notes Documents Return of the Fed Login

Code

*More fully fledged applications can be found on the Projects page. The Bash page is dedicated to fully commented oneliners, and a MySQL quick reference is available here.



<?php
	/**
	 * Will Bergen - 2018
	 * Some useful PDO based wrappers for PHP
	 */


	/**
	 * Returns 1 or more rows in $tbl where $where = $arg1
	 * Uses PDO->query()
	 * SELECT * FROM table; w/ my_query_get(table, 1, 1)
	 * Do NOT extraneously quote $arg1
	 * @param  [String] $tbl   [Table to query]
	 * @param  [String] $where [Conditonal field]
	 * @param  [String] $arg1  [Value $where must match]
	 * @return [Array/String]  [A single array (row), an array of arrays (rows), or "!err" on failure]
	 */
	function my_query_get($tbl, $where, $arg1) {

		require("connect_pdo.php");

		$query = "SELECT * FROM " .
			$tbl . " WHERE " .
			$where . "='" .
			$arg1 . "'";

		// Setup Statement:
		$stmt = $pdo->query($query);
		
		$result = $stmt->fetchAll();
		if (count($result) > 0) {
			return $result;
		} else {
			return "!err";
		}
	}

	/**
	 * Get the contents of $field from $tbl where $where = $arg1
	 * Use PDO->query()
	 * Will only return a single field, if condition matches multiple rows, my_query_get_value will not succeed
	 * Do NOT extraneously quote $arg1
	 * @param  [String] $tbl   [Table to query]
	 * @param  [String] $field [Which field's to get]
	 * @param  [String] $where [Conditional field]
	 * @param  [String] $arg1  [Value $where must match]
	 * @return [String]        [Returns value on success, else "!err"]
	 */
	function my_query_get_value($tbl, $field, $where, $arg1) {

		require("connect_pdo.php");

		$query = "SELECT " . 
			$field . " FROM " .
			$tbl . " WHERE " .
			$where . "='" .
			$arg1 . "'";

		// Debuggin:
		// echo $query;

		$stmt = $pdo->query($query);
		
		$result = $stmt->fetchAll();
		if (count($result) == 1) {
			return $result[0][$field];
		} else {
			return "!err";
		}
	}

	/**
	 * Updates a specific field in a row. sets the $field to $content, WHERE $where = $arg1
	 * Uses PDO w/ prepared statement
	 * @param  [String] $tbl     [Table to update]
	 * @param  [String] $field   [Specific field to update]
	 * @param  [String] $where   [Conditional field]
	 * @param  [String] $arg1    [Value $where must match]
	 * @param  [String] $content [New field contents/value]
	 * @return [Bool]  	         [True on success, else False]
	 */
	function my_query_set_value($tbl, $field, $where, $arg1, $content) {

		require("connect_pdo.php");

		// Debuggin:
		// $q = sprintf("UPDATE %s SET %s=? WHERE %s='%s'", 
		//	$tbl, $field, $where, $arg1);
		// echo $q;

		$stmt = $pdo->prepare(sprintf("UPDATE %s SET %s=? WHERE %s='%s'",
			$tbl, $field, $where, $arg1));

		if ($stmt->execute(array($content))){
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Inserts a row into $tbl, setting ($flds) = ($vals)
	 * Uses PDO w/ prepared statements
	 * @param  [String] $tbl  [Table to modify]
	 * @param  [Array]  $flds [Array of fields]
	 * @param  [Array]  $vals [Array of content/values]
	 * @return [Bool]         [True on success, else False]
	 */
	function my_query_insert($tbl, $flds, $vals) {
		require('connect_pdo.php');

		// Enforce same length arrarys
		if (count($flds) != count($vals)){return false;}

		// Debuggin:
		// $q = sprintf("INSERT INTO %s (%s) VALUES (%s);",
		// 	$tbl,
		// 	implode(", ",$flds),
		// 	substr(str_repeat("?, ", count($flds)), 0, -2));
		// echo $q;

		$stmt = $pdo->prepare(sprintf("INSERT INTO %s (%s) VALUES (%s)",
			$tbl,
			implode(", ",$flds),
			substr(str_repeat("?, ", count($flds)), 0, -2) // (?, ?, ...)
		));

		if ($stmt->execute($vals)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Updates a row.  sets the $flds passed to $vals, WHERE $where = $cond
	 * Uses PDO w/ prepared statement
	 * @param  [String] $tbl   Table to update
	 * @param  [Array]  $flds  Array of fields
	 * @param  [Array]  $vals  Array of values
	 * @param  [String] $where Conditional field
	 * @param  [String] $cond  Value $where must match
	 * @return [Bool]          True on success, else False
	 */
	function my_query_update($tbl, $flds, $vals, $where, $cond) {
		require('connect_pdo.php');

		// Enforce same length arrarys
		if (count($flds) != count($vals)){return false;}

		// Generate the string of field1=?, field2=?, ..
		$t = "";
		for ($i=0; $i < count($flds); $i++) { 
			$t .= $flds[$i] . " = " . "?, ";
		}
		$t = substr($t, 0, -2);

		// Debuggin:
		// $q = sprintf("UPDATE %s SET %s WHERE %s=%s",
			// $tbl, $t, $where, $cond);
		// echo $q;

		$stmt = $pdo->prepare(sprintf("UPDATE %s SET %s WHERE %s=%s", $tbl, $t, $where, $cond)
		);

		if ($stmt->execute($vals)){
			return true;
		} else {
			return false;
		}
	}

?>