ARRAY // Execute a DB command function db_cmd($query) { $stmt = $GLOBALS['pdo']->query($query); if (is_bool($stmt)) { return $stmt; } else { return $stmt->fetch(); } } // ------------------------------------- // STRING STRING --> ARRAY // Return all values of a specific column function db_get_columns($table, $column) { return db_cmd("select " . $column . " from " . $table); } // STRING STRING VARYING --> ARRAY // Return all rows that have an 'identifier' column set to given value function db_get_rows($table, $identifier, $value) { return db_cmd("select * from " . $table . " where " . $identifier . " = " . $value . ";"); } // STRING STRING VARYING STRING --> ARRAY // Return the value of a specific column in a given row, identified by an // 'identifier' column set to the given value function db_get_cell($table, $identifier, $value, $cell) { return db_get_rows($table, $identifier, $value)[$cell]; } // -------------------------------------- // STRING STRING VARYING STRING VARYING --> NIL // Edit the value of a cell function db_set_cell($table, $identifier, $value, $cell, $new_value) { if (is_string($value)) { $value = "'" . $value . "'"; } if (is_string($new_value)) { $new_value = "'" . $new_value . "'"; } return db_cmd("update " . $table . " set " . $cell . " = " . $new_value . " where " . $identifier . " = " . $value) . ";"; } // ------------------------------------- // STRING ARRAY ARRAY --> BOOLEAN // Create a table with given values to given columns. // First array is a list of columns (as would be provided to SQL), and the // second is the list of values (as would follow " values " in SQL) function db_insert_row($table, $variables, $values) { $variables = comma_sep($variables); $values = comma_sep(strings_wrap($values)); return db_cmd("insert into " . $table . " (". $variables .")" . " values (" . $values . ")" . ";"); } // ------------------------------------- // STRING --> BOOLEAN // Return whether or not a table of given name exists function db_table_existant($table) { if (!db_cmd("select * from information_schema.tables " . " where table_name = " . string_wrap($table) . ";")) { return false; } else { return true; } } // STRING ARRAY --> BOOLEAN // Create a table of given name and columns (with array of column-strings) function db_create_table($table, $columns) { return db_cmd("create table " . $table . " (" . comma_sep($columns) . ");"); } ?>