MySQL

Create a MySQLi connection


/*
 * Create a MySQLi connection 
 * and fetch data from a table.
 */

// Set host and credentials.
$host = '127.0.0.1';
$database = 'db';
$user = 'user';
$password = 'pass';

// Create and check connection.
$connection = mysqli_connect($host, $user, $password, $database);
if(!$connection)
  exit('Could not establish connection ' . mysqli_connect_error());

// Create a simple query.
$items = $connection->query('SELECT * FROM table');

// See number of items to handle.
print 'Number of items fetched: ' . $items->num_rows;

Create a MySQL user and grant permissions

CREATE USER 'someUser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON someDatabase.* TO 'someUser'@'localhost';
FLUSH PRIVILEGES;

Create a users table


CREATE TABLE `DATABASENAME`.`users` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `email_UNIQUE` (`email` ASC));
Subscribe to RSS - MySQL