Database

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;

Install CouchDB on Ubuntu

CouchDB can be installed by typing the following command in command line:

sudo apt-get install couchdb

​You can see if CouchDB is installed properly by typing sudo service couchdb status in command line.

couchdb_is_installed.png

Once CouchDB is installed, a web interface is available on http://localhost:5984/_utils/

Install SQLite on Ubuntu

SQLite can be installed by typing the following command in command line:

sudo apt-get install sqlite3 libsqlite3-dev

You can see if SQLite is installed properly by typing sqlite3 in command line.

sqlite3_is_installed.png

Quit SQLite by typing .exit.

Help is on the way when typing .help.

 

Drop MongoDB database in PHP


// Establish connection.
$connection = new Mongo();

// Select database.
$database = $connection->demo;

// Drop database.
$dropped = $database->drop();
var_dump($dropped);

// Close connection.
$connection->close();

First steps using MongoDB in PHP


// Establish connection.
$connection = new Mongo();
// Select database, create database if database not exists.
$database = $connection->demo;
// Select collection.
$collection = $database->users;
// Insert data.
$user = array(
  'name' => 'Dribbit',
  'email' => 'info@dribbit.eu',
);
$collection->insert($user);

// Get all records.
$records = $collection->find();
var_dump($records);
foreach($records as $record) {
  var_dump($record);
}

// Close connection.
$connection->close();

Where does mongodb stores data

Type following command in command line terminal:

sudo nano /etc/mongodb.conf

 

mongo_conf.png

This is based on an Ubuntu system.

Install MongoDB driver for PHP on Ubuntu

Type following line in command line:

sudo pecl install mongo

If Pecl throws this error: configure: error: sasl.h not found! add following command and retry installing mongodb

sudo apt-get install libsasl2-dev

After installing, add the mongo extension to php.ini

extension=mongo.so

Restart apache:

Install MongoDB on Ubuntu

32 bit systems are limited to DB of 2 gig. Therefor, use MongoDB only on 64 bit systems in production.

@see MongoDb documentation to install MongoDB.

See if MongoDB is running by typing

sudo service mongodb status

in command line.

 

Pages

Subscribe to RSS - Database