Quick Start

Connecting and doing a query:

1
2
3
4
5
6
7
<?php

require_once 'dalmp.php';

$db = new DALMP\database('utf8://root@localhost');

$rs = $db->FetchMode('ASSOC')->GetAssoc('SHOW VARIABLES LIKE "char%"');

Note

DALMP is the name of the namespace

Will output something like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Array
(
    [character_set_client] => utf8
    [character_set_connection] => utf8
    [character_set_database] => latin1
    [character_set_filesystem] => binary
    [character_set_results] => utf8
    [character_set_server] => latin1
    [character_set_system] => utf8
    [character_sets_dir] => /usr/local/mysql-5.6.10-osx10.7-x86/share/charsets/
)

DALMP\Database takes the parameters from a DNS (database source name) so before you can start using it you need to define this values.

DSN format

charset://username:password@host:port/database

When using Unix domain sockets:

charset://username:password@unix_socket=\path\of\the.socket/database
  • Notice that the path of the socket is using backslashes.
\path\of\the.socket

Will be translated to:

/path/of/the.socket

DSN Cache format

charset://username:password@host:port/database?(type:host:port:compression)
type:Memcache, Redis, Disk.
host:The host of the Memcache, Redis server.
port:The port of the Memcache, Redis server.
compression:To use or not compression, only available for memcache.

See also

DALMP\Database Cache method.

Common methods

The next table contains, 5 common methods for retrieving data:

Name Normal Prepared Statements Cache Normal Cache Prepared Statements
all GetAll PGetAll CacheGetAll CachePGetAll
assoc GetAssoc PGetAssoc CacheGetAssoc CachePGetAssoc
col GetCol PGetCol CacheGetCol CachePGetCol
one GetOne PGetOne CacheGetOne CachePGetOne
row GetRow PGetRow CacheGetRow CachePGetRow

For Inserting or Updating, you can use the Execure or PExecute methods.

DALMP Classes

For better code maintainability, DALMP is formed by different classes, the main class and the one that does the abstraction layer is DALMP\Database.

mysql DALMP\Database
cache DALMP\Cache
queue DALMP\Queue
sessions DALMP\Sessions
DI DALMP\DI

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

$user = getenv('MYSQL_USER') ?: 'root';
$password = getenv('MYSQL_PASS') ?: '';

require_once 'dalmp.php';

$DSN = "utf8://$user:$password@127.0.0.1/test";

$db = new DALMP\Database($DSN);

try {
    $rs = $db->getOne('SELECT now()');
} catch (\Exception $e) {
    print_r($e->getMessage());
}

/**
 * 1 log to single file
 * 2 log to multiple files (creates a log per request)
 * 'off' to stop debuging
 */
$db->debug(1);

echo $db, PHP_EOL; // print connection details

If you wan to use the system default charset the DSN would be:

1
$DSN = "mysql://$user:$password@127.0.0.1/test";
  • notice the mysql:// instead of the utf8://

SSL

If you want to use SSL, an array containing the SSL parameters must be passed as the second argument to the database method example:

1
2
3
4
5
$ssl = array('key' => null, 'cert' => null, 'ca' => 'mysql-ssl.ca-cert.pem', 'capath' => null, 'cipher' => null);

$DSN = 'latin1://root:secret@127.0.0.1/test';

$db = new DALMP\Database($DSN, $ssl);

The $ssl array argument, must follow this format:

key:The path name to the key file.
cert:The path name to the certificate file.
ca:The path name to the certificate authority file.
capath:The pathname to a directory that contains trusted SSL CA certificates in PEM format.
cipher:A list of allowable ciphers to use for SSL encryption.

Note

When using SSL, PHP OpenSSL support must be enable for this to work.

To check that your connection has SSL you can test with this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

require_once 'dalmp.php';

$ssl = array('key' => null, 'cert' => null, 'ca' => 'mysql-ssl.ca-cert.pem', 'capath' => null, 'cipher' => null);

$DSN = 'utf8://root:secret@127.0.0.1/test';

$db = new DALMP\Database($DSN, $ssl);

try {
  $db->getOne('SELECT NOW()');
  print_r($db->FetchMode('ASSOC')->GetRow("show variables like 'have_ssl'"));
} catch (\Exception $e) {
  print_r($e->getMessage());
}

try {
  print_r($db->GetRow("show status like 'ssl_cipher'"));
} catch (\Exception $e) {
  print_r($e->getMessage());
}

If you have SSL you will get something like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Array
(
  [Variable_name] => have_ssl
  [Value] => YES
)

Array
(
  [Variable_name] => Ssl_cipher
  [Value] => DHE-RSA-AES256-SHA
)

Otherwise:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Array
(
  [Variable_name] => have_ssl
  [Value] => DISABLED
)

Array
(
  [Variable_name] => Ssl_cipher
  [Value] =>
)

Example using a socket

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

require_once 'dalmp.php';

$user = getenv('MYSQL_USER') ?: 'root';
$password = getenv('MYSQL_PASS') ?: '';

$DSN = "utf8://$user:$password".'@unix_socket=\tmp\mysql.sock/test';

$db = new DALMP\Database($DSN);

$db->debug(1);

try {
  echo PHP_EOL, 'example using unix_socket: ', $db->getOne('SELECT NOW()'), PHP_EOL;
} catch (\Exception $e) {
  print_r($e->getMessage());
}

echo $db;
# will print: DALMP :: connected to: db, Character set: utf8, Localhost via UNIX socket,...

Example using cache (memcache)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php

require_once 'dalmp.php';

$user = getenv('MYSQL_USER') ?: 'root';
$password = getenv('MYSQL_PASS') ?: '';

$DSN = "utf8://$user:$password".'@localhost/test';

$db = new DALMP\Database($DSN);

$cache = new DALMP\Cache(new DALMP\Cache\Memcache());

$db->useCache($cache);

$rs = $db->CacheGetOne('SELECT now()');

echo $rs, PHP_EOL;

Example using DSN cache (redis)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

require_once 'dalmp.php';

$user = getenv('MYSQL_USER') ?: 'root';
$password = getenv('MYSQL_PASS') ?: '';

$DSN = "utf8://$user:$password".'@localhost/dalmp?redis:127.0.0.1:6379';

$db = new DALMP\Database($DSN);

$db->FetchMode('ASSOC');

$rs = $db->CacheGetAll('SELECT * FROM City');

echo $rs, PHP_EOL;

See also

DALMP Examples

Thanks Navicat for supporting Open Source projects.

Navicat



A great amount of time has been spent creating, crafting and maintaining this software, please consider donating.

Donating helps ensure continued support, development and availability.

dalmp


comments powered by Disqus