1<?php 2// Measure Page Load Time 3require_once '../../MPLT.php'; 4$timer = new MPLT(); 5# ------------------------------------------------------------------------------ 6$user = getenv('MYSQL_USER') ?: 'root'; 7$password = getenv('MYSQL_PASS') ?: ''; 8 9/** 10 * 11 * For using DALMP you need a Data Source Name (DSN) 12 * 13 * The DSN format is: 14 * 15 * charset://username:password@host:port/database 16 * 17 * or if using socket: 18 * 19 * charset://username:password@unix_socket=\path\of\the.socket/database 20 * 21 * Notice that the path of the socket is using backslashes. 22 * 23 * \path\of\the.socket will be translated to /path/of/the.socket 24 * 25 * If you want to use your system default charset, use 'mysql' as the charset. 26 * 27 * see bellow some examples. 28 */ 29 30/** 31 * require the DALMP class 32 */ 33require_once '../../src/dalmp.php'; 34 35/** 36 * example of a simple connection 37 * 38 * charset: default system 39 * user: dalmp 40 * password: password 41 * host: 192.168.1.40 42 * database: dalmptest 43 * 44 */ 45$db = new DALMP\Database('mysql://dalmp:password@192.168.1.40/dalmptest'); 46try { 47 $rs = $db->getOne('SELECT now()'); 48} catch (\Exception $e) { 49 print_r($e->getMessage()); 50} 51 52/** 53 * 1 log to single file 54 * 2 log to multiple files (creates a log per request) 55 * 'off' to stop debuging 56 */ 57$db->debug(1); 58 59echo $db, PHP_EOL; // print connection details 60 61/** 62 * example of a connection using UTF8 charset 63 * 64 * charset: utf8 65 * user: dalmp 66 * password: password 67 * host: 127.0.0.1 68 * port: 3306 69 * database: dalmptest 70 */ 71$db = new DALMP\Database('utf8://dalmp:password@127.0.0.1:3306/dalmptest'); 72try { 73 $db->getOne('SELECT now()'); 74} catch (\Exception $e) { 75 print_r($e->getMessage()); 76} 77 78echo PHP_EOL, $db, PHP_EOL; // will print: DALMP :: connected to: db2, Character set: utf8, 127.0.0.1 via TCP/IP, Server version: ... 79 80/** 81 * example using SSL (OpenSSL support must be enabled for this to work) 82 * 83 * charset: latin1 84 * user: root 85 * password: mysql 86 * host: 127.0.0.1 87 * database: dalmp 88 * 89 * An array containing the SSL parameters must be passed as the second argument to the database method: 90 * 91 * $db = new DALMP(DSN, $ssl_array); 92 * 93 * key = The path name to the key file. 94 * cert = The path name to the certificate file. 95 * ca = The path name to the certificate authority file. 96 * capath = The pathname to a directory that contains trusted SSL CA certificates in PEM format. 97 * cipher = A list of allowable ciphers to use for SSL encryption. 98 * 99 */ 100$ssl = array('key' => null, 'cert' => null, 'ca' => 'mysql-ssl.ca-cert.pem', 'capath' => null, 'cipher' => null); 101$db = new DALMP\Database('latin1://root:mysql@127.0.0.1/dalmp', $ssl); 102 103try { 104 $db->getOne('SELECT NOW()'); 105 print_r($db->FetchMode('ASSOC')->GetRow("show variables like 'have_ssl'")); 106} catch (\Exception $e) { 107 print_r($e->getMessage()); 108} 109 110/** 111 * If you have SSL will get something like: 112Array 113( 114 [Variable_name] => have_ssl 115 [Value] => YES 116) 117* otherwise 118* 119Array 120( 121 [Variable_name] => have_ssl 122 [Value] => DISABLED 123) 124 */ 125 126try { 127 print_r($db->GetRow("show status like 'ssl_cipher'")); 128} catch (\Exception $e) { 129 print_r($e->getMessage()); 130} 131 132/** 133 * IF SSL working you should see something similar to this: 134Array 135( 136 [Variable_name] => Ssl_cipher 137 [Value] => DHE-RSA-AES256-SHA 138) 139* otherwise 140Array 141( 142 [Variable_name] => Ssl_cipher 143 [Value] => 144) 145 */ 146 147/** 148 * example using a socket for the connection 149 * 150 * charset: utf8 151 * user: $user 152 * password: $password 153 * socket path: /tmp/mysql.sock 154 * database: dalmp 155 */ 156$db = new DALMP\Database("utf8://$user:$password".'@unix_socket=\tmp\mysql.sock/dalmp'); 157$db->debug(1); 158try { 159 echo PHP_EOL, 'example using unix_socket: ', $db->getOne('SELECT NOW()'), PHP_EOL; 160} catch (\Exception $e) { 161 print_r($e->getMessage()); 162} 163 164echo $db; // will print: DALMP :: connected to: db4, Character set: utf8, Localhost via UNIX socket,... 165 166# ----------------------------------------------------------------------------------------------------------------- 167echo PHP_EOL,str_repeat('-', 80),PHP_EOL,'Time: ',$timer->getPageLoadTime(),' - Memory: ',$timer->getMemoryUsage(1),PHP_EOL,str_repeat('-', 80),PHP_EOL; 168