4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2012 The Nucleus Group
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * (see nucleus/documentation/index.html#license for more info)
14 * @license http://nucleuscms.org/license.txt GNU General Public License
15 * @copyright Copyright (C) 2012 The Nucleus Group
19 * sql_* wrappers for DB class access
21 if ( !class_exists('DB') )
23 include_once realpath(dirname(__FILE__)) . '/DB.php';
27 * Connects to database server with arguments
30 function sql_connect_args($host = 'localhost', $user = '', $password = '', $database = '', $new_link = FALSE)
32 global $MYSQL_HANDLER;
33 return DB::setConnectionInfo($MYSQL_HANDLER[1], $host, $user, $password, $database);
37 * Connects to database server
40 function sql_connect()
42 global $MYSQL_HANDLER, $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE;
44 return DB::setConnectionInfo($MYSQL_HANDLER[1], $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE);
48 * disconnects from SQL server
51 function sql_disconnect($conn = false)
57 * disconnects from SQL server
59 * @param unknown_type $conn
61 function sql_close($conn = false)
67 * executes an SQL query
70 function sql_query($query, $conn = false)
72 global $mysql_affected_row;
73 $mysql_affected_row = -1;
74 if ( preg_match('/^\W*(INSERT|UPDATE|DELETE|DROP)/i', $query) )
76 $mysql_affected_row = DB::execute($query);
77 return $mysql_affected_row !== FALSE ? TRUE : FALSE;
79 return DB::getResult($query);
83 * executes an SQL error
86 function sql_error($conn = false)
88 $err = DB::getError();
93 * executes an SQL db select
96 function sql_select_db($db, $conn = false)
103 * executes an SQL real escape
106 function sql_real_escape_string($val, $conn = false)
108 if ( is_numeric($val) )
112 $escapeval = DB::quoteValue($val);
113 if ( preg_match("/^'(.*)'$/", $escapeval, $matches) )
115 $escapeval = $matches[1];
121 * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
124 function sql_quote_string($val, $conn = false)
126 if ( is_numeric($val) )
130 return DB::quoteValue($val);
134 * executes an SQL insert id
137 function sql_insert_id($conn = false)
139 return DB::getInsertId();
143 * executes an SQL result request
146 function sql_result($res, $row, $col)
148 if ( !method_exists($res, 'fetch') ) return FALSE;
149 for ( $i = 0; $i < $row; $i++ )
151 return $res->fetchColumn($col);
155 * frees sql result resources
158 function sql_free_result($res)
160 if ( !method_exists($res, 'closeCursor') ) return FALSE;
161 return $res->closeCursor();
165 * returns number of rows in SQL result
168 function sql_num_rows($res)
170 if ( !method_exists($res, 'rowCount') ) return FALSE;
171 return $res->rowCount();
175 * returns number of rows affected by SQL query
178 function sql_affected_rows($conn = false)
180 global $mysql_affected_row;
181 return $mysql_affected_row;
185 * Get number of fields in result
188 function sql_num_fields($res)
190 if ( !method_exists($res, 'columnCount') ) return FALSE;
191 return $res->columnCount();
195 * fetches next row of SQL result as an associative array
198 function sql_fetch_assoc($res)
200 if ( !method_exists($res, 'fetch') ) return FALSE;
201 return $res->fetch(PDO::FETCH_ASSOC);
205 * Fetch a result row as an associative array, a numeric array, or both
208 function sql_fetch_array($res)
210 if ( !method_exists($res, 'fetch') ) return FALSE;
211 return $res->fetch(PDO::FETCH_BOTH);
215 * fetches next row of SQL result as an object
218 function sql_fetch_object($res)
220 if ( !method_exists($res, 'fetch') ) return FALSE;
221 return $res->fetch(PDO::FETCH_OBJ);
225 * Get a result row as an enumerated array
228 function sql_fetch_row($res)
230 if ( !method_exists($res, 'fetch') ) return FALSE;
231 return $res->fetch(PDO::FETCH_NUM);
235 * Get column information from a result and return as an object
238 function sql_fetch_field($res, $offset = 0)
244 * Get current system status (returns string)
247 function sql_stat($conn = false)
253 * Returns the name of the character set
256 function sql_client_encoding($conn = false)
262 * Get SQL client version
265 function sql_get_client_info()
267 return DB::getAttribute(PDO::ATTR_CLIENT_VERSION);
271 * Get SQL server version
274 function sql_get_server_info($conn = false)
276 return DB::getAttribute(PDO::ATTR_SERVER_VERSION);
280 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
283 function sql_get_host_info($conn = false)
289 * Returns the SQL protocol on success, or FALSE on failure.
292 function sql_get_proto_info($conn = false)
298 * Get the name of the specified field in a result
301 function sql_field_name($res, $offset = 0)
303 if ( !method_exists($res, 'getColumnMeta') ) return FALSE;
304 $column = $res->getColumnMeta($offset);
305 return $column['name'];
309 * Set character encodings in each fields related to MySQL connection.
312 function sql_set_charset($charset)