Recommend this page to a friend! |
Classes of Justin Eldracher | MySQL Database Library | README.md | Download |
|
Downloadphp-database-libraryPHP library for easy access to MySQL databases and the file system - or at least that's what it used to be! It has become sort of a melting pot of everything now... ;) WARNING: This is still a work in progress, so be carefull when updating!!This project was begun by Justin Eldracher in 2015. Feel free to do whatever you want to this code, just please let me know what you changed/added so I don't miss out on anything awesome! ;) If you find any errors or would like to make comments, please leave a message at: http://blindwarrior.16mb.com/writemsg.php Changes:
MySQLi support coming soon!Method List:init([default table]);Initializes the database connection and selects a database. Sets the table also, if one is given as a parameter.
destroy();Closes the database connection. getall();Returns an associative matrix of the databases and their tables on the current MySQL connection.
gtables([db_name]);Returns an array of the tables in the given database or the active one, if no parameter is given.
setdb(new_database_name);Change the active database.
getdb();Returns the name of the active database.
stable(new_table_name);Change the active table.
gtable();Returns the name of the active table.
setprime(new_primary_key);Changes the default Primary Key.
getprime();Returns the current default Primary Key.
columns([custom table]);Returns an array of the column names in a table, default to active table of course.
all([column to sort by], [sort order], [custom table]);Returns either the entire active table or a custom table as a matrix.
select([columns to select, either string or array], [assoc array for WHERE], [column to sort by], ["ASC" or "DESC"], ["=", "<="...], ["AND", "OR", "NOT"] [custom table]);Returns a matrix for a SELECT statement, default is the entire table. Has quite a bit of power... ;)
search(text_to_find, [column(s) to look in, either string or array], [custom table], [match type]);Returns a matrix of the matches found for the first variable. The second paramter can be a string of one column name, or an array of column names to look in. The default is to search in all the columns of the table.
toarray(mysql_result);Returns a matrix of a db result.
insert(array_or_matrix_of_values, [custom table]);Inserts an array or matrix into the active or custom table. Array length must match the number of columns in the table.
sqlstr(mixed_var);Adds single quotes around a string variable if it doesn't have them already. All variables used in SQL queries are run through this function automatically.
update(assoc_array_values_to_change, assoc_array_for_WHERE_statement, [custom table]);Updates a custom table or the active one using two associative arrays.
delete([identifier column, default to _prime_ variable], value_to_match, [boolean, default true]);Deletes a row based on the value of the first parameter.
authorize(user_name, password);Checks if the given username and password are in the users table and unique. If so, returns an associative array of that row of the users table, otherwise returns false.
addauthuser(array_of_values, [custom index for password, default 1]);Adds an array of values to the users table. First parameter MUST NOT include a Primary Key as the first item. Second parameter is the index of the password in the array of values.
updateuser(assoc_array_values_to_change, assoc_array_for_WHERE_statement);Updates a record in the users table.
chgpass(current_user, current_password, new_user, new_password);Updates only the username and password, if given username and password already exist.
deluser(numeric_id);Deletes a user.
addsetting(array_of_values);Inserts a row into the settings table.
getsettings(["array" or "json", default "array"]);Returns either an associative array of the settings from the settings table or a json string.
savesetting(id, new_value);Changes the value of a setting by it's id.
tocss(output_file_name, [matrix of values, default is settings table])Takes a matrix and turn it into a css file, written to specified file path.
shift([starting id, default 1], [custom table]);Shifts all rows of a table down, starting at the primary key specified.
reset([column], [custom table]);Resets a certain column of the specified table, by default the primary key and the active table. Only works for numerical fields, obviously. Ideal for keeping the highest primary key equal to the number of records in the database. For example: | id | name | |:--:|:----:| |1|Justin| |2|Frank| |5|Michael| |7|Joseph| After the | id | name | |:--:|:----:| |1|Justin| |2|Frank| |3|Michael| |4|Joseph|
execute([sql query], [custom table]);Executes a given SQL query or a select all by default.
rows([sql query], [custom table]);Returns the number of rows in an sql query, default is the entire table.
File System Functions:read(file_name, [return type], [custom delimeter for type "csv"]);Reads a file and return contents based on value of the second parameter, which can be:
"string", "array", "xml", "doc", "pdf", "csv", "zip" or "docx". Default is "string".
If the
write(file_name, file_contents, [custom delimeter for csv files]);Writes content to a file. If content is an array, file will be saved in delimited format.
ren(old_name, new_name);Renames a file.
del(file_name, [directory]);Deletes a file or directory. file_name can also be a regular expression, and when it is the method will delete all the files in the current directory or the one specified in the second parameter that match the regular expression.
fcopy(source, destination);Copies a file or folder.
is(file_name);Simplify file exists.
getfiles([directory, defaults to root], [regular expression for desired files], [boolean for sorted, default true]);Returns an array of all the files and directories in a directory based on a regular expression for desired files. If no parameters are given, will return the contents of the directory containing your script.
getpath(file_name, [desired segment]);Parses a file's path into segments by directory and returns an array, by default. If an array is given, will convert it into a file path. The second parameter is the index number of the desired fragment, can be negative to start at the end.
filename(file_name);Remove unwanted characters from a string to make a safe file name.
Miscellaneous Functions:get(input_variable, [send method: "post" or "get", default "post"]);Returns a variable sent through either POST or GET.
datestring(date_string);Returns: "Friday, April 3<sup>rd</sup>, 2015" for "5/3/15".
cookie(name, [value]);Multipurpose function for dealing with cookies. The three possible uses are listed below. This method uses a "clever hack" ;) to get around a cookie's default behavior of not being available until after the next http request. Running the three statements below would print out the value as expected.
escregx(string, [modifiers, eg. "i"]);Escapes all regular expression special characters with a backslash and wraps in forward slashes. The the string is already wrapped in forward slashes, nothing happens.
Table Structure:All tables are assumed to have a Primary Key column named "id". The users table must have the first column for the Primary Key, the second column for the username (named "user"), and the third column for the password (named "pass"). Other columns may be added after the first three.
The Settings table is something I have found convenient for customizing cms systems. | id | name | value | alias | selector | |:--:|:----:|:-----:|:-----:|:--------:| | 1 | color | #ff0000 | Text Color: | body | | ... | ... | ... | ... | ... |... | Coupled with
Configuration:
For ease of updating and using on local and remote servers, save the following code (with whatever additional configuration changes you want) as a new class.
|