PHP   30
mysql php
Guest on 27th August 2023 06:01:30 AM


  1. <?php
  2.  
  3. /*
  4.  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
  5.  * Copyright (C)  The Nucleus Group
  6.  *
  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)
  12.  */
  13. /**
  14.  * @license http://nucleuscms.org/license.txt GNU General Public License
  15.  * @copyright Copyright (C) 2002-2009 The Nucleus Group
  16.  * @version $Id$
  17.  */
  18.  
  19. /*
  20.  * complete sql_* wrappers for mysql functions
  21.  *
  22.  * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
  23.  */
  24.  
  25.  
  26. $MYSQL_CONN = 0;
  27.  
  28. if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
  29. {
  30.         /**
  31.          * Errors before the database connection has been made
  32.          */
  33.         function startUpError($msg, $title) {
  34.                 ?>
  35.                 <html xmlns="http://www.w3.org/1999/xhtml">
  36.                         <head><title><?php echo htmlspecialchars($title)?></title></head>
  37.                         <body>
  38.                                 <h1><?php echo htmlspecialchars($title)?></h1>
  39.                                 <?php echo $msg?>
  40.                         </body>
  41.                 </html>
  42. <?php
  43.                 exit;
  44.         }
  45.        
  46.         /**
  47.           * Connects to mysql server with arguments
  48.           */
  49.         function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
  50.                
  51.                 $CONN = mysql_connect($mysql_host, $mysql_user, $mysql_password);
  52.                 if ($mysql_database) {
  53.                         $charset = _getDbCharset($mysql_database);
  54.                         if($charset) mysql_query("SET NAMES '$charset'");
  55.                         mysql_select_db($mysql_database,$CONN);
  56.                 }
  57.  
  58.                 return $CONN;
  59.         }
  60.        
  61.         /**
  62.           * Connects to mysql server
  63.           */
  64.         function sql_connect() {
  65.                 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN;
  66.  
  67.                 $MYSQL_CONN = mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or startUpError('<p>Could not connect to MySQL database.</p>', 'Connect Error');
  68.                 $charset = _getDbCharset($MYSQL_DATABASE);
  69.                 if($charset) mysql_query("SET NAMES '$charset'");
  70.                 mysql_select_db($MYSQL_DATABASE) or startUpError('<p>Could not select database: ' . mysql_error() . '</p>', 'Connect Error');
  71.  
  72.                 return $MYSQL_CONN;
  73.         }
  74.  
  75.         /**
  76.          * Get DB default charset
  77.          */
  78.         function _getDbCharset($db) {
  79.                 if (!$db) {
  80.                         return false;
  81.                 }
  82.  
  83.                 // We use a SHOW CREATE DATABASE command to show the original
  84.                 // SQL character set when DB was created.
  85.                 $result = mysql_query("SHOW CREATE DATABASE `$db`");
  86.                 if (mysql_num_rows($result) < 0 ) {
  87.                         // The specified db name is wrong!
  88.                         return false;
  89.                 }
  90.                 $dbInfo = mysql_fetch_row($result);
  91.                 $pattern = '/40100 DEFAULT CHARACTER SET (\w+) /';
  92.                 if ( (preg_match($pattern, $dbInfo[1], $match) > 0) ) {
  93.                         return $match[1];
  94.                 }
  95.                 return false;
  96.         }
  97.  
  98.         /**
  99.           * disconnects from SQL server
  100.           */
  101.         function sql_disconnect($conn = false) {
  102.                 global $MYSQL_CONN;
  103.                 if (!$conn) $conn = $MYSQL_CONN;
  104.                 mysql_close($conn);
  105.         }
  106.        
  107.         function sql_close($conn = false) {
  108.                 global $MYSQL_CONN;
  109.                 if (!$conn) $conn = $MYSQL_CONN;
  110.                 mysql_close($conn);
  111.         }
  112.        
  113.         /**
  114.           * executes an SQL query
  115.           */
  116.         function sql_query($query,$conn = false) {
  117.                 global $SQLCount,$MYSQL_CONN;
  118.                 if (!$conn) $conn = $MYSQL_CONN;
  119.                 $SQLCount++;
  120.                 $res = mysql_query($query,$conn) or print("mySQL error with query $query: " . mysql_error($conn) . '<p />');
  121.                 return $res;
  122.         }
  123.        
  124.         /**
  125.           * executes an SQL error
  126.           */
  127.         function sql_error($conn = false)
  128.         {
  129.                 global $MYSQL_CONN;
  130.                 if (!$conn) $conn = $MYSQL_CONN;
  131.                 return mysql_error($conn);
  132.         }
  133.        
  134.         /**
  135.           * executes an SQL db select
  136.           */
  137.         function sql_select_db($db,$conn = false)
  138.         {
  139.                 global $MYSQL_CONN;
  140.                 if (!$conn) $conn = $MYSQL_CONN;
  141.                 return mysql_select_db($db,$conn);
  142.         }
  143.        
  144.         /**
  145.           * executes an SQL real escape
  146.           */
  147.         function sql_real_escape_string($val,$conn = false)
  148.         {
  149.                 global $MYSQL_CONN;
  150.                 if (!$conn) $conn = $MYSQL_CONN;
  151.                 return mysql_real_escape_string($val,$conn);
  152.         }
  153.        
  154.         /**
  155.           * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
  156.           */
  157.         function sql_quote_string($val,$conn = false) {
  158.                 global $MYSQL_CONN;
  159.                 if (!$conn) $conn = $MYSQL_CONN;
  160.                 return "'".mysql_real_escape_string($val,$conn)."'";
  161.         }
  162.        
  163.         /**
  164.           * executes an SQL insert id
  165.           */
  166.         function sql_insert_id($conn = false)
  167.         {
  168.                 global $MYSQL_CONN;
  169.                 if (!$conn) $conn = $MYSQL_CONN;
  170.                 return mysql_insert_id($conn);
  171.         }
  172.        
  173.         /**
  174.           * executes an SQL result request
  175.           */
  176.         function sql_result($res, $row, $col)
  177.         {
  178.                 return mysql_result($res,$row,$col);
  179.         }
  180.        
  181.         /**
  182.           * frees sql result resources
  183.           */
  184.         function sql_free_result($res)
  185.         {
  186.                 return mysql_free_result($res);
  187.         }
  188.        
  189.         /**
  190.           * returns number of rows in SQL result
  191.           */
  192.         function sql_num_rows($res)
  193.         {
  194.                 return mysql_num_rows($res);
  195.         }
  196.        
  197.         /**
  198.           * returns number of rows affected by SQL query
  199.           */
  200.         function sql_affected_rows($conn = false)
  201.         {
  202.                 global $MYSQL_CONN;
  203.                 if (!$conn) $conn = $MYSQL_CONN;
  204.                 return mysql_affected_rows($conn);
  205.         }
  206.        
  207.         /**
  208.           * Get number of fields in result
  209.           */
  210.         function sql_num_fields($res)
  211.         {
  212.                 return mysql_num_fields($res);
  213.         }
  214.        
  215.         /**
  216.           * fetches next row of SQL result as an associative array
  217.           */
  218.         function sql_fetch_assoc($res)
  219.         {
  220.                 return mysql_fetch_assoc($res);
  221.         }
  222.        
  223.         /**
  224.           * Fetch a result row as an associative array, a numeric array, or both
  225.           */
  226.         function sql_fetch_array($res)
  227.         {
  228.                 return mysql_fetch_array($res);
  229.         }
  230.        
  231.         /**
  232.           * fetches next row of SQL result as an object
  233.           */
  234.         function sql_fetch_object($res)
  235.         {
  236.                 return mysql_fetch_object($res);
  237.         }
  238.        
  239.         /**
  240.           * Get a result row as an enumerated array
  241.           */
  242.         function sql_fetch_row($res)
  243.         {
  244.                 return mysql_fetch_row($res);
  245.         }
  246.        
  247.         /**
  248.           * Get column information from a result and return as an object
  249.           */
  250.         function sql_fetch_field($res,$offset = 0)
  251.         {
  252.                 return mysql_fetch_field($res,$offset);
  253.         }
  254.        
  255.         /**
  256.           * Get current system status (returns string)
  257.           */
  258.         function sql_stat($conn=false)
  259.         {
  260.                 global $MYSQL_CONN;
  261.                 if (!$conn) $conn = $MYSQL_CONN;
  262.                 return mysql_stat($conn);
  263.         }
  264.        
  265.         /**
  266.           * Returns the name of the character set
  267.           */
  268.         function sql_client_encoding($conn=false)
  269.         {
  270.                 global $MYSQL_CONN;
  271.                 if (!$conn) $conn = $MYSQL_CONN;
  272.                 return mysql_client_encoding($conn);
  273.         }
  274.        
  275.         /**
  276.           * Get SQL client version
  277.           */
  278.         function sql_get_client_info()
  279.         {
  280.                 return mysql_get_client_info();
  281.         }
  282.        
  283.         /**
  284.           * Get SQL server version
  285.           */
  286.         function sql_get_server_info($conn=false)
  287.         {
  288.                 global $MYSQL_CONN;
  289.                 if (!$conn) $conn = $MYSQL_CONN;
  290.                 return mysql_get_server_info($conn);
  291.         }
  292.        
  293.         /**
  294.           * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
  295.           */
  296.         function sql_get_host_info($conn=false)
  297.         {
  298.                 global $MYSQL_CONN;
  299.                 if (!$conn) $conn = $MYSQL_CONN;
  300.                 return mysql_get_host_info($conn);
  301.         }
  302.        
  303.         /**
  304.           * Returns the SQL protocol on success, or FALSE on failure.
  305.           */
  306.         function sql_get_proto_info($conn=false)
  307.         {
  308.                 global $MYSQL_CONN;
  309.                 if (!$conn) $conn = $MYSQL_CONN;
  310.                 return mysql_get_proto_info($conn);
  311.         }
  312.  
  313.     /**
  314.      * Get the name of the specified field in a result
  315.      */
  316.     function sql_field_name($res, $offset = 0)
  317.     {
  318.         return mysql_field_name($res, $offset);
  319.     }
  320.        
  321. /**************************************************************************
  322. Unimplemented mysql_* functions
  323.  
  324. # mysql_ data_ seek (maybe useful)
  325. # mysql_ errno (maybe useful)
  326. # mysql_ fetch_ lengths (maybe useful)
  327. # mysql_ field_ flags (maybe useful)
  328. # mysql_ field_ len (maybe useful)
  329. # mysql_ field_ seek (maybe useful)
  330. # mysql_ field_ table (maybe useful)
  331. # mysql_ field_ type (maybe useful)
  332. # mysql_ info (maybe useful)
  333. # mysql_ list_ processes (maybe useful)
  334. # mysql_ ping (maybe useful)
  335. # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
  336. # mysql_ thread_ id (maybe useful)
  337.  
  338. # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
  339. # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
  340.  
  341. # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
  342. # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
  343.  
  344. # mysql_ change_ user (deprecated)
  345. # mysql_ create_ db (deprecated)
  346. # mysql_ db_ query (deprecated)
  347. # mysql_ drop_ db (deprecated)
  348. # mysql_ escape_ string (deprecated)
  349. # mysql_ list_ fields (deprecated)
  350. # mysql_ list_ tables (deprecated)
  351. # mysql_ tablename (deprecated)
  352.  
  353. *******************************************************************/
  354.  
  355.  
  356. }
  357. ?>

Raw Paste

Login or Register to edit or fork this paste. It's free.