Yet Another small update. :)
[iDB.git] / inc / misc / sql / mysqli.php
blobc75a61910b4ad6a74397127a9bcfaae133d3e311
1 <?php
2 /*
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the Revised BSD License.
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 Revised BSD License for more details.
11 Copyright 2004-2010 iDB Support - http://idb.berlios.de/
12 Copyright 2004-2010 Game Maker 2k - http://gamemaker2k.org/
14 $FileInfo: mysqli.php - Last Update: 10/14/2010 SVN 588 - Author: cooldude2k $
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="mysqli.php"||$File3Name=="/mysqli.php") {
18 @header('Location: index.php');
19 exit(); }
20 // MySQL Functions.
21 function sql_error($link=null) {
22 if(isset($link)) {
23 $result = mysqli_error($link); }
24 if(!isset($link)) {
25 $result = mysqli_error(); }
26 if ($result=="") {
27 return ""; }
28 return $result; }
29 function sql_errno($link=null) {
30 if(isset($link)) {
31 $result = mysqli_errno($link); }
32 if(!isset($link)) {
33 $result = mysqli_errno(); }
34 if ($result===0) {
35 return 0; }
36 return $result; }
37 function sql_errorno($link=null) {
38 if(isset($link)) {
39 $result = sql_error($link);
40 $resultno = sql_errno($link); }
41 if(!isset($link)) {
42 $result = sql_error();
43 $resultno = sql_errno(); }
44 if ($result==""&&$result===0) {
45 return ""; }
46 if ($result!=""&&$result!==0) {
47 $result = $resultno.": ".$result; }
48 return $result; }
49 // Execute a query :P
50 $NumQueries = 0;
51 function sql_query($query,$link=null) {
52 global $NumQueries;
53 if(isset($link)) {
54 $result = mysqli_query($link,$query); }
55 if(!isset($link)) {
56 $result = mysqli_query(null,$query); }
57 if ($result===false) {
58 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
59 return false; }
60 if ($result!==false) {
61 ++$NumQueries;
62 return $result; } }
63 //Fetch Number of Rows
64 function sql_num_rows($result) {
65 $num = mysqli_num_rows($result);
66 if ($num===false) {
67 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
68 return false; }
69 return $num; }
70 // Connect to mysql database
71 function sql_connect_db($server,$username,$password,$database=null,$new_link=false) {
72 $myport = "3306";
73 $hostex = explode(":", $server);
74 if(isset($hostex[1])&&
75 !is_numeric($hostex[1])) {
76 $hostex[1] = $myport; }
77 if(isset($hostex[1])) {
78 $server = $hostex[0];
79 $myport = $hostex[1]; }
80 if($database===null) {
81 $link = mysqli_connect($server,$username,$password,null,$myport); }
82 if($database!==null) {
83 $link = mysqli_connect($server,$username,$password,$database,$myport); }
84 if ($link===false) {
85 output_error("MySQLi Error ".mysqli_connect_errno().": ".mysqli_connect_error(),E_USER_ERROR);
86 return false; }
87 $result = sql_query("SET SESSION SQL_MODE='ANSI_QUOTES,NO_AUTO_VALUE_ON_ZERO';",$link);
88 if ($result===false) {
89 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
90 return false; }
91 return $link; }
92 // Query Results :P
93 function sql_result($result,$row,$field=0) {
94 $check = mysqli_data_seek($result,$row);
95 if ($check===false) {
96 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
97 return false; }
98 $trow = mysqli_fetch_array($result);
99 $retval = $trow[$field];
100 return $retval; }
101 // Free Results :P
102 function sql_free_result($result) {
103 $fresult = mysqli_free_result($result);
104 if ($fresult===false) {
105 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
106 return false; }
107 if ($fresult===true) {
108 return true; } }
109 //Fetch Results to Array
110 function sql_fetch_array($result,$result_type=MYSQLI_BOTH) {
111 $row = mysqli_fetch_array($result,$result_type);
112 return $row; }
113 //Fetch Results to Associative Array
114 function sql_fetch_assoc($result) {
115 $row = mysqli_fetch_assoc($result);
116 return $row; }
117 //Fetch Row Results
118 function sql_fetch_row($result) {
119 $row = mysqli_fetch_row($result);
120 return $row; }
121 //Get Server Info
122 function sql_server_info($link=null) {
123 if(isset($link)) {
124 $result = mysqli_get_server_info($link); }
125 if(!isset($link)) {
126 $result = mysqli_get_server_info(); }
127 return $result; }
128 //Get Client Info
129 function sql_client_info($link=null) {
130 $result = mysqli_get_client_info();
131 return $result; }
132 function sql_escape_string($string,$link=null) {
133 global $SQLStat;
134 if(isset($link)) {
135 $string = mysqli_real_escape_string($link,$string); }
136 if(!isset($link)) {
137 $string = mysqli_real_escape_string($SQLStat,$string); }
138 if ($string===false) {
139 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
140 return false; }
141 return $string; }
142 // SafeSQL Lite Source Code by Cool Dude 2k
143 // Make SQL Query's safe
144 function sql_pre_query($query_string,$query_vars) {
145 $query_array = array(array("%i","%I","%F","%S"),array("%d","%d","%f","%s"));
146 $query_string = str_replace($query_array[0], $query_array[1], $query_string);
147 if (get_magic_quotes_gpc()) {
148 $query_vars = array_map("stripslashes", $query_vars); }
149 $query_vars = array_map("sql_escape_string", $query_vars);
150 $query_val = $query_vars;
151 $query_num = count($query_val);
152 $query_i = 0;
153 while ($query_i < $query_num) {
154 $query_is = $query_i+1;
155 $query_val[$query_is] = $query_vars[$query_i];
156 ++$query_i; }
157 $query_val[0] = $query_string;
158 return call_user_func_array("sprintf",$query_val); }
159 function sql_set_charset($charset,$link=null) {
160 if(function_exists('mysqli_set_charset')===false) {
161 if(!isset($link)) {
162 $result = sql_query("SET CHARACTER SET '".$charset."'"); }
163 if(isset($link)) {
164 $result = sql_query("SET CHARACTER SET '".$charset."'",$link); }
165 if ($result===false) {
166 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
167 return false; }
168 if(!isset($link)) {
169 $result = sql_query("SET NAMES '".$charset."'"); }
170 if(isset($link)) {
171 $result = sql_query("SET NAMES '".$charset."'",$link); }
172 if ($result===false) {
173 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
174 return false; }
175 return true; }
176 if(function_exists('mysqli_set_charset')===true) {
177 if(isset($link)) {
178 $result = mysqli_set_charset($link,$charset); }
179 if(!isset($link)) {
180 $result = mysqli_set_charset(null,$charset); }
181 if ($result===false) {
182 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
183 return false; }
184 return true; } }
186 if(function_exists('mysqli_set_charset')===false) {
187 function mysqli_set_charset($charset,$link) {
188 if(isset($link)) {
189 $result = sql_set_charset($charset,$link); }
190 if(!isset($link)) {
191 $result = sql_set_charset($charset); }
192 if ($result===false) {
193 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
194 return false; }
195 return true; } }
197 // Get next id for stuff
198 function sql_get_next_id($tablepre,$table,$link=null) {
199 $nid = mysqli_insert_id($link);
200 return $nid; }
201 // Get number of rows for table
202 function sql_get_num_rows($tablepre,$table,$link=null) {
203 $getnextidq = sql_pre_query("SHOW TABLE STATUS LIKE '".$tablepre.$table."'", array());
204 if(!isset($link)) {
205 $getnextidr = sql_query($getnextidq); }
206 if(isset($link)) {
207 $getnextidr = sql_query($getnextidq,$link); }
208 $getnextid = sql_fetch_assoc($getnextidr);
209 return $getnextid['Rows'];
210 @sql_free_result($getnextidr); }