Small update
[iDB.git] / inc / misc / sql / mysqli.php
blobe524ecf3e9df22e4aa87aff05c618297e2e7d4ba
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-2019 iDB Support - https://idb.osdn.jp/support/category.php?act=view&id=1
12 Copyright 2004-2019 Game Maker 2k - https://idb.osdn.jp/support/category.php?act=view&id=2
14 $FileInfo: mysqli.php - Last Update: 08/02/2019 SVN 905 - 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 function sql_disconnect_db($link=null) {
93 return mysqli_close($link); }
94 // Query Results :P
95 function sql_result($result,$row,$field=0) {
96 $check = mysqli_data_seek($result,$row);
97 if ($check===false) {
98 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
99 return false; }
100 $trow = mysqli_fetch_array($result);
101 if(!isset($trow[$field])) { $trow[$field] = null; }
102 $retval = $trow[$field];
103 return $retval; }
104 // Free Results :P
105 function sql_free_result($result) {
106 $fresult = mysqli_free_result($result);
107 if ($fresult===false) {
108 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
109 return false; }
110 if ($fresult===true) {
111 return true; } }
112 //Fetch Results to Array
113 function sql_fetch_array($result,$result_type=MYSQLI_BOTH) {
114 $row = mysqli_fetch_array($result,$result_type);
115 return $row; }
116 //Fetch Results to Associative Array
117 function sql_fetch_assoc($result) {
118 $row = mysqli_fetch_assoc($result);
119 return $row; }
120 //Fetch Row Results
121 function sql_fetch_row($result) {
122 $row = mysqli_fetch_row($result);
123 return $row; }
124 //Get Server Info
125 function sql_server_info($link=null) {
126 if(isset($link)) {
127 $result = mysqli_get_server_info($link); }
128 if(!isset($link)) {
129 $result = mysqli_get_server_info(); }
130 return $result; }
131 //Get Client Info
132 function sql_client_info($link=null) {
133 $result = mysqli_get_client_info();
134 return $result; }
135 function sql_escape_string($string,$link=null) {
136 global $SQLStat;
137 if(isset($link)) {
138 $string = mysqli_real_escape_string($link,$string); }
139 if(!isset($link)) {
140 $string = mysqli_real_escape_string($SQLStat,$string); }
141 if ($string===false) {
142 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
143 return false; }
144 return $string; }
145 // SafeSQL Lite Source Code by Cool Dude 2k
146 // Make SQL Query's safe
147 function sql_pre_query($query_string,$query_vars) {
148 $query_array = array(array("%i","%I","%F","%S"),array("%d","%d","%f","%s"));
149 $query_string = str_replace($query_array[0], $query_array[1], $query_string);
150 if (get_magic_quotes_gpc()) {
151 $query_vars = array_map("stripslashes", $query_vars); }
152 $query_vars = array_map("sql_escape_string", $query_vars);
153 $query_val = $query_vars;
154 $query_num = count($query_val);
155 $query_i = 0;
156 while ($query_i < $query_num) {
157 $query_is = $query_i+1;
158 $query_val[$query_is] = $query_vars[$query_i];
159 ++$query_i; }
160 $query_val[0] = $query_string;
161 return call_user_func_array("sprintf",$query_val); }
162 function sql_set_charset($charset,$link=null) {
163 if(function_exists('mysqli_set_charset')===false) {
164 if(!isset($link)) {
165 $result = sql_query("SET CHARACTER SET '".$charset."'"); }
166 if(isset($link)) {
167 $result = sql_query("SET CHARACTER SET '".$charset."'",$link); }
168 if ($result===false) {
169 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
170 return false; }
171 if(!isset($link)) {
172 $result = sql_query("SET NAMES '".$charset."'"); }
173 if(isset($link)) {
174 $result = sql_query("SET NAMES '".$charset."'",$link); }
175 if ($result===false) {
176 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
177 return false; }
178 return true; }
179 if(function_exists('mysqli_set_charset')===true) {
180 if(isset($link)) {
181 $result = mysqli_set_charset($link,$charset); }
182 if(!isset($link)) {
183 $result = mysqli_set_charset(null,$charset); }
184 if ($result===false) {
185 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
186 return false; }
187 return true; } }
189 if(function_exists('mysqli_set_charset')===false) {
190 function mysqli_set_charset($charset,$link) {
191 if(isset($link)) {
192 $result = sql_set_charset($charset,$link); }
193 if(!isset($link)) {
194 $result = sql_set_charset($charset); }
195 if ($result===false) {
196 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
197 return false; }
198 return true; } }
200 // Get next id for stuff
201 function sql_get_next_id($tablepre,$table,$link=null) {
202 $nid = mysqli_insert_id($link);
203 return $nid; }
204 // Get number of rows for table
205 function sql_get_num_rows($tablepre,$table,$link=null) {
206 $getnextidq = sql_pre_query("SHOW TABLE STATUS LIKE '".$tablepre.$table."'", array());
207 if(!isset($link)) {
208 $getnextidr = sql_query($getnextidq); }
209 if(isset($link)) {
210 $getnextidr = sql_query($getnextidq,$link); }
211 $getnextid = sql_fetch_assoc($getnextidr);
212 return $getnextid['Rows'];
213 @sql_free_result($getnextidr); }