{"id":16221516,"url":"https://github.com/a1phanumeric/php-mysql-class","last_synced_at":"2025-05-14T21:05:36.196Z","repository":{"id":1017141,"uuid":"844688","full_name":"a1phanumeric/PHP-MySQL-Class","owner":"a1phanumeric","description":"Simple MySQL class written in PHP, for interfacing with a MySQL database.","archived":false,"fork":false,"pushed_at":"2025-01-17T11:31:48.000Z","size":70,"stargazers_count":364,"open_issues_count":20,"forks_count":254,"subscribers_count":45,"default_branch":"master","last_synced_at":"2025-04-13T16:08:10.705Z","etag":null,"topics":["database","mysql","pdo","php","php-mysql"],"latest_commit_sha":null,"homepage":"https://rockfire.co","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/a1phanumeric.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2010-08-17T20:39:51.000Z","updated_at":"2025-01-17T11:30:28.000Z","dependencies_parsed_at":"2025-01-31T06:36:57.412Z","dependency_job_id":null,"html_url":"https://github.com/a1phanumeric/PHP-MySQL-Class","commit_stats":{"total_commits":58,"total_committers":28,"mean_commits":"2.0714285714285716","dds":0.7586206896551724,"last_synced_commit":"9c189d039a68211828100bcd592fc09cb057df86"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a1phanumeric%2FPHP-MySQL-Class","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a1phanumeric%2FPHP-MySQL-Class/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a1phanumeric%2FPHP-MySQL-Class/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a1phanumeric%2FPHP-MySQL-Class/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/a1phanumeric","download_url":"https://codeload.github.com/a1phanumeric/PHP-MySQL-Class/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741206,"owners_count":21154255,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","mysql","pdo","php","php-mysql"],"created_at":"2024-10-10T12:08:19.889Z","updated_at":"2025-04-13T16:08:17.989Z","avatar_url":"https://github.com/a1phanumeric.png","language":"PHP","readme":"Important Notice\n===============\n\nAs of December 2014 I decided to upload the PHP MySQL Class I wrote a while back, and now use on a daily basis. It's PDO based (the `mysql_*` functions were due to be deprecated quite a while back now!).\n\nThe old version is still a part of this repo for now, and the readme is still available [here](class.MySQL.README.md).\n\n\n\nPHP MySQL Class\n===============\n\nThis is a simple to use MySQL class that easily bolts on to any existing PHP application, streamlining your MySQL interactions.\n\nSetup v2.0+\n-----\n\nInclude the class using composer as below:\n\n`composer require a1phanumeric/php-mysql-class`\n\nTo use in your project, use the following:\n\n`use A1phanumeric\\DBPDO;`\n\n`$DB = new DBPDO('db_host', 'db_name', 'db_user', 'db_pass');`\n\nOr, if wanting to use as a singleton instance:\n\n`$DB = DBPDO::getInstance('db_host', 'db_name', 'db_user', 'db_pass');`\n\n### Setup Before v2.0\n-----\n\nFirstly, define four constants for the host, database name, username and password:\n\n`define('DATABASE_NAME', 'my_database');`\n\n`define('DATABASE_USER', 'username');`\n\n`define('DATABASE_PASS', 'password');`\n\n`define('DATABASE_HOST', 'localhost');`\n\nThen, simply include this class into your project like so:\n\n`include_once('/path/to/class.DBPDO.php');`\n\nThen invoke the class:\n\n`$DB = new DBPDO();`\n\n\nDirect Queries\n-----\n\nTo perform direct queries where you don't need to return any results (such as update, insert etc...), just do the following:\n\n`$DB-\u003eexecute(\"UPDATE customers SET email = 'newemail@domain.com' WHERE username = 'a1phanumeric'\");`\n\nThat's the easiest way to use the class, but we should be utilising prepared statements now. This means no more escaping shizzle! To utilise prepared statements, just change the above code to the following:\n\n`$DB-\u003eexecute(\"UPDATE customers SET email = ? WHERE username = ?\", array('newemail@domain.com', 'a1phanumeric'));`\n\nThe class will invoke PDO's prepared statements and put the email and username in their place respectively, as well as escape all values passed to it. **Note:** You don't need to put the speechmarks in on the query, the **?** is enough, and PDO will sort that out for you.\n\n\nFetching Rows\n-----\n\nTo perform select queries with this class, the syntax is similar to the above, but we have two functions we can utilise, `fetch` and `fetchAll`.\n\n`fetch` simply returns one row, useful for getting a user by their ID for example. This returns an associative array and looks like:\n\n`$user = $DB-\u003efetch(\"SELECT * FROM users WHERE id = ?\", $id);`\n\nNow `$user` will contain an array of the fields for the row where there query matches. Oh, what's that? We didn't pass an array as the second parameter we just passed a single variable? That's cool, the class will treat a single variable the same as if you passed `array($id)`. It's just a handy little time-saver.\n\n`fetchAll` is used to fetch multiple rows, the parameters are similar, but the result returns an array of records:\n\n`$counties = $DB-\u003efetchAll(\"SELECT * FROM counties\");`\n\nThe above will return a list of counties (in the UK) in my database like so:\n\n```\n[0] =\u003e Array\n(\n    [id] =\u003e 1\n    [county] =\u003e London\n)\n\n[1] =\u003e Array\n(\n    [id] =\u003e 2\n    [county] =\u003e Bedfordshire\n)\n\n[2] =\u003e Array\n(\n    [id] =\u003e 3\n    [county] =\u003e Buckinghamshire\n)\n```\n\nHowever, what if I want to loop over some raw data and check if the data matches the county name? To do that means either looping over these results every time, or shifting the key to the root dimension of the multi-dimensional array. However, if we pass a third variable, we can have that column as the key:\n\n`$counties = $DB-\u003efetchAll(\"SELECT * FROM counties\", null, 'county');`\n\n**Note:** I passed null as the second paramater as we're not passing any variables into the query to be escaped.\n\nThis will now return an array like the following:\n\n```\n[London] =\u003e Array\n(\n    [id] =\u003e 1\n    [county] =\u003e London\n)\n\n[Bedfordshire] =\u003e Array\n(\n    [id] =\u003e 2\n    [county] =\u003e Bedfordshire\n)\n\n[Buckinghamshire] =\u003e Array\n(\n    [id] =\u003e 3\n    [county] =\u003e Buckinghamshire\n)\n```\n\nSo of course we could now do something like:\n\n`if(isset($counties[$raw_data['county_name']])){ //Do something }`\n\n### License\n\nThis program is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program.  If not, see \u003chttp://www.gnu.org/licenses/\u003e.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa1phanumeric%2Fphp-mysql-class","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fa1phanumeric%2Fphp-mysql-class","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa1phanumeric%2Fphp-mysql-class/lists"}