All Projects → a1phanumeric → Php Mysql Class

a1phanumeric / Php Mysql Class

Simple MySQL class written in PHP, for interfacing with a MySQL database.

Projects that are alternatives of or similar to Php Mysql Class

Php Sql Query Builder
An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation.
Stars: ✭ 313 (-10.32%)
Mutual labels:  pdo, database, mysql
Leafpub
Simple, beautiful, open source publishing.
Stars: ✭ 645 (+84.81%)
Mutual labels:  pdo, database, mysql
Dibi
Dibi - smart database abstraction layer
Stars: ✭ 373 (+6.88%)
Mutual labels:  pdo, database, mysql
Mysqldump Php
PHP version of mysqldump cli that comes with MySQL
Stars: ✭ 975 (+179.37%)
Mutual labels:  pdo, database, mysql
Easydb
Easy-to-use PDO wrapper for PHP projects.
Stars: ✭ 624 (+78.8%)
Mutual labels:  pdo, database, mysql
Pdo
Connecting to MySQL in PHP using PDO.
Stars: ✭ 187 (-46.42%)
Mutual labels:  pdo, database, mysql
Dbq
Zero boilerplate database operations for Go
Stars: ✭ 273 (-21.78%)
Mutual labels:  database, mysql
Node Orm2
Object Relational Mapping
Stars: ✭ 3,063 (+777.65%)
Mutual labels:  database, mysql
Sql exporter
Database agnostic SQL exporter for Prometheus
Stars: ✭ 301 (-13.75%)
Mutual labels:  database, mysql
Server
EQEmu - Open Source EverQuest Server
Stars: ✭ 319 (-8.6%)
Mutual labels:  database, mysql
Php Pdo Mysql Class
A PHP MySQL PDO class similar to the the Python MySQLdb, which supports iterator and parameter binding when using "WHERE IN" statement.
Stars: ✭ 213 (-38.97%)
Mutual labels:  pdo, mysql
Laconia
🏺 ‎ A minimalist MVC framework.
Stars: ✭ 307 (-12.03%)
Mutual labels:  database, mysql
Architect
A set of tools which enhances ORMs written in Python with more features
Stars: ✭ 320 (-8.31%)
Mutual labels:  database, mysql
Pg chameleon
MySQL to PostgreSQL replica system
Stars: ✭ 274 (-21.49%)
Mutual labels:  database, mysql
Sequelizer
A GUI Desktop App for export sequelize models from database automatically.
Stars: ✭ 273 (-21.78%)
Mutual labels:  database, mysql
Crud
Relational database library for SQL databases & Go.
Stars: ✭ 296 (-15.19%)
Mutual labels:  database, mysql
Wetland
A Node.js ORM, mapping-based. Works with MySQL, PostgreSQL, SQLite and more.
Stars: ✭ 261 (-25.21%)
Mutual labels:  database, mysql
Rdbc
Rust DataBase Connectivity (RDBC) :: Common Rust API for database drivers
Stars: ✭ 328 (-6.02%)
Mutual labels:  database, mysql
Crecto
Database wrapper and ORM for Crystal, inspired by Ecto
Stars: ✭ 325 (-6.88%)
Mutual labels:  database, mysql
Koa Vue Notes Api
🤓 This is a simple SPA built using Koa as the backend, Vue as the first frontend, and React as the second frontend. Features MySQL integration, user authentication, CRUD note actions, and async/await.
Stars: ✭ 342 (-2.01%)
Mutual labels:  database, mysql

Important Notice

As 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!).

The old version is still a part of this repo for now, and the readme is still available here.

PHP MySQL Class

This is a simple to use MySQL class that easily bolts on to any existing PHP application, streamlining your MySQL interactions.

Setup

Firstly, define four constants for the host, database name, username and password:

define('DATABASE_NAME', 'my_database');

define('DATABASE_USER', 'username');

define('DATABASE_PASS', 'password');

define('DATABASE_HOST', 'localhost');

Then, simply include this class into your project like so:

include_once('/path/to/class.DBPDO.php');

Then invoke the class:

$DB = new DBPDO();

Direct Queries

To perform direct queries where you don't need to return any results (such as update, insert etc...), just do the following:

$DB->execute("UPDATE customers SET email = '[email protected]' WHERE username = 'a1phanumeric'");

That'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:

$DB->execute("UPDATE customers SET email = ? WHERE username = ?", array('[email protected]', 'a1phanumeric'));

The 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.

Fetching Rows

To perform select queries with this class, the syntax is similar to the above, but we have two functions we can utilise, fetch and fetchAll.

fetch simply returns one row, useful for getting a user by their ID for example. This returns an associative array and looks like:

$user = $DB->fetch("SELECT * FROM users WHERE id = ?", $id);

Now $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.

fetchAll is used to fetch multiple rows, the parameters are similar, but the result returns an array of records:

$counties = $DB->fetchAll("SELECT * FROM counties");

The above will return a list of counties (in the UK) in my database like so:

[0] => Array
(
    [id] => 1
    [county] => London
)

[1] => Array
(
    [id] => 2
    [county] => Bedfordshire
)

[2] => Array
(
    [id] => 3
    [county] => Buckinghamshire
)

However, 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:

$counties = $DB->fetchAll("SELECT * FROM counties", null, 'county');

Note: I passed null as the second paramater as we're not passing any variables into the query to be escaped.

This will now return an array like the following:

[London] => Array
(
    [id] => 1
    [county] => London
)

[Bedfordshire] => Array
(
    [id] => 2
    [county] => Bedfordshire
)

[Buckinghamshire] => Array
(
    [id] => 3
    [county] => Buckinghamshire
)

So of course we could now do something like:

if(isset($counties[$raw_data['county_name']])){ //Do something }

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].