1: <?php
2: //============================================================+
3: // File name : tce_db_dal_mysql.php
4: // Begin : 2003-10-12
5: // Last Update : 2014-03-31
6: //
7: // Description : MySQL driver for TCExam Database Abstraction
8: // Layer (DAL).
9: // This abstraction use the same SQL syntax
10: // of MySQL.
11: //
12: // Author: Nicola Asuni
13: //
14: // (c) Copyright:
15: // Nicola Asuni
16: // Tecnick.com LTD
17: // www.tecnick.com
18: // info@tecnick.com
19: //
20: // License:
21: // Copyright (C) 2004-2014 Nicola Asuni - Tecnick.com LTD
22: // See LICENSE.TXT file for more information.
23: //============================================================+
24:
25: /**
26: * @file
27: * MySQL driver for TCExam Database Abstraction Layer (DAL).
28: * This abstraction layer uses the same SQL syntax of MySQL.
29: * @package com.tecnick.tcexam.shared
30: * @author Nicola Asuni
31: * @since 2003-10-12
32: */
33:
34: /**
35: * Open a connection to a MySQL Server and select a database.
36: * @param $host (string) database server host name.
37: * @param $port (string) database connection port
38: * @param $username (string) Name of the user that owns the server process.
39: * @param $password (string) Password of the user that owns the server process.
40: * @param $database (string) Database name.
41: * @return MySQL link identifier on success, or FALSE on failure.
42: */
43: function F_db_connect($host = 'localhost', $port = '3306', $username = 'root', $password = '', $database = '')
44: {
45: if (!$db = @mysql_connect($host.':'.$port, $username, $password)) {
46: return false;
47: }
48: if ((strlen($database) > 0) and (!@mysql_select_db($database, $db))) {
49: return false;
50: }
51: // set the correct charset encoding
52: mysql_query('SET NAMES \'utf8\'');
53: mysql_query('SET CHARACTER_SET \'utf8\'');
54: mysql_query('SET collation \'utf8_unicode_ci\'');
55: mysql_query('SET collation_server \'utf8_unicode_ci\'');
56: mysql_query('SET collation_database \'utf8_unicode_ci\'');
57: return $db;
58: }
59:
60: /**
61: * Closes the non-persistent connection to a database associated with the given connection resource.
62: * @param $link_identifier (resource) database link identifier.
63: * @return bool TRUE on success or FALSE on failure
64: */
65: function F_db_close($link_identifier)
66: {
67: return mysql_close($link_identifier);
68: }
69:
70: /**
71: * Returns the text of the error message from previous database operation
72: * @return string error message.
73: */
74: function F_db_error($link_identifier = null)
75: {
76: if (empty($link_identifier)) {
77: return '';
78: }
79: return '['.mysql_errno($link_identifier).']: '.mysql_error($link_identifier).'';
80:
81: }
82:
83: /**
84: * Sends a query to the currently active database on the server that's associated with the specified link identifier.<br>
85: * @param $query (string) The query tosend. The query string should not end with a semicolon.
86: * @param $link_identifier (resource) database link identifier.
87: * @return FALSE in case of error, TRUE or resource-identifier in case of success.
88: */
89: function F_db_query($query, $link_identifier)
90: {
91: // convert PostgreSQL RANDOM() function to MySQL RAND()
92: //$query = preg_replace("/ORDER BY RANDOM\(\)/i", "ORDER BY RAND()", $query);
93: return mysql_query($query, $link_identifier);
94: }
95:
96: /**
97: * Fetch a result row as an associative and numeric array.
98: * Note: This function sets NULL fields to PHP NULL value.
99: * @param $result (resource) result resource to the query result.
100: * @return Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.
101: */
102: function F_db_fetch_array($result)
103: {
104: return mysql_fetch_array($result);
105: }
106:
107: /**
108: * Fetch a result row as an associative array.
109: * Note: This function sets NULL fields to PHP NULL value.
110: * @param $result (resource) result resource to the query result.
111: * @return Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.
112: */
113: function F_db_fetch_assoc($result)
114: {
115: return mysql_fetch_assoc($result);
116: }
117:
118: /**
119: * Returns number of rows (tuples) affected by the last INSERT, UPDATE or DELETE query associated with link_identifier.
120: * @param $link_identifier (resource) database link identifier.
121: * @param $result (resource) result resource to the query result [UNUSED].
122: * @return Number of rows.
123: */
124: function F_db_affected_rows($link_identifier, $result)
125: {
126: return mysql_affected_rows($link_identifier);
127: }
128:
129: /**
130: * Get number of rows in result.
131: * @param $result (resource) result resource to the query result.
132: * @return Number of affected rows.
133: */
134: function F_db_num_rows($result)
135: {
136: return mysql_num_rows($result);
137: }
138:
139: /**
140: * Get the ID generated from the previous INSERT operation
141: * @param $link_identifier (resource) database link identifier.
142: * @param $tablename (string) Table name.
143: * @param $fieldname (string) Field name (column name).
144: * @return int ID generated from the last INSERT operation.
145: */
146: function F_db_insert_id($link_identifier, $tablename = '', $fieldname = '')
147: {
148: /*
149: * NOTE : mysql_insert_id() converts the return type of the
150: * native MySQL C API function mysql_insert_id() to a type
151: * of long (named int in PHP). If your AUTO_INCREMENT column
152: * has a column type of BIGINT, the value returned by
153: * mysql_insert_id() will be incorrect.
154: */
155: //return mysql_insert_id($link_identifier);
156: if ($r = mysql_query('SELECT LAST_INSERT_ID() FROM '.$tablename.'', $link_identifier)) {
157: if ($m = mysql_fetch_row($r)) {
158: return $m[0];
159: }
160: }
161: return 0;
162: }
163:
164: /**
165: * Escape a string for insertion into a SQL text field (avoiding SQL injection).
166: * @param $link_identifier (resource) database link identifier.
167: * @param $str (string) The string that is to be escaped.
168: * @param $stripslashes (boolean) if true strip slashes from string
169: * @return string Returns the escaped string, or FALSE on error.
170: * @since 5.0.005 2007-12-05
171: */
172: function F_escape_sql($link_identifier, $str, $stripslashes = true)
173: {
174: // Reverse magic_quotes_gpc/magic_quotes_sybase effects if ON.
175: if ($stripslashes) {
176: $str = stripslashes($str);
177: }
178: return mysql_real_escape_string($str, $link_identifier);
179: }
180:
181: //============================================================+
182: // END OF FILE
183: //============================================================+
184: