Small update
[iDB.git] / inc / misc / sql / mysql.php
blob0a8ab9ddad3da7c6f5ac0ba45f8758283f14ecb4
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: mysql.php - Last Update: 08/02/2019 SVN 905 - Author: cooldude2k $
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="mysql.php"||$File3Name=="/mysql.php") {
18 @header('Location: index.php');
19 exit(); }
20 // MySQL Functions.
21 function sql_error($link=null) {
22 if(isset($link)) {
23 $result = mysql_error($link); }
24 if(!isset($link)) {
25 $result = mysql_error(); }
26 if ($result=="") {
27 return ""; }
28 return $result; }
29 function sql_errno($link=null) {
30 if(isset($link)) {
31 $result = mysql_errno($link); }
32 if(!isset($link)) {
33 $result = mysql_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 = mysql_query($query,$link); }
55 if(!isset($link)) {
56 $result = mysql_query($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 = mysql_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 if($new_link!==true) { $new_link = false; }
73 if($new_link!==true||$new_link===false) {
74 $link = mysql_connect($server,$username,$password); }
75 if($new_link===true) {
76 $link = mysql_connect($server,$username,$password,$new_link); }
77 if ($link===false) {
78 output_error("Not connected: ".sql_error(),E_USER_ERROR);
79 return false; }
80 if($database!==null) {
81 $dlink = mysql_select_db($database,$link);
82 if ($dlink===false) {
83 output_error("Can't use database ".$database.": ".sql_error(),E_USER_ERROR);
84 return false; } }
85 $result = sql_query("SET SESSION SQL_MODE='ANSI_QUOTES,NO_AUTO_VALUE_ON_ZERO';",$link);
86 if ($result===false) {
87 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
88 return false; }
89 return $link; }
90 function sql_disconnect_db($link=null) {
91 return mysql_close($link); }
92 // Query Results :P
93 function sql_result($result,$row,$field=0) {
94 $value = mysql_result($result, $row, $field);
95 if ($value===false) {
96 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
97 return false; }
98 return $value; }
99 // Free Results :P
100 function sql_free_result($result) {
101 $fresult = mysql_free_result($result);
102 if ($fresult===false) {
103 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
104 return false; }
105 if ($fresult===true) {
106 return true; } }
107 //Fetch Results to Array
108 function sql_fetch_array($result,$result_type=MYSQL_BOTH) {
109 $row = mysql_fetch_array($result,$result_type);
110 return $row; }
111 //Fetch Results to Associative Array
112 function sql_fetch_assoc($result) {
113 $row = mysql_fetch_assoc($result);
114 return $row; }
115 //Fetch Row Results
116 function sql_fetch_row($result) {
117 $row = mysql_fetch_row($result);
118 return $row; }
119 //Get Server Info
120 function sql_server_info($link=null) {
121 if(isset($link)) {
122 $result = mysql_get_server_info($link); }
123 if(!isset($link)) {
124 $result = mysql_get_server_info(); }
125 return $result; }
126 //Get Client Info
127 function sql_client_info($link=null) {
128 $result = mysql_get_client_info();
129 return $result; }
130 function sql_escape_string($string,$link=null) {
131 if(isset($link)) {
132 $string = mysql_real_escape_string($string,$link); }
133 if(!isset($link)) {
134 $string = mysql_real_escape_string($string); }
135 if ($string===false) {
136 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
137 return false; }
138 return $string; }
139 // SafeSQL Lite Source Code by Cool Dude 2k
140 // Make SQL Query's safe
141 function sql_pre_query($query_string,$query_vars) {
142 $query_array = array(array("%i","%I","%F","%S"),array("%d","%d","%f","%s"));
143 $query_string = str_replace($query_array[0], $query_array[1], $query_string);
144 if (get_magic_quotes_gpc()) {
145 $query_vars = array_map("stripslashes", $query_vars); }
146 $query_vars = array_map("sql_escape_string", $query_vars);
147 $query_val = $query_vars;
148 $query_num = count($query_val);
149 $query_i = 0;
150 while ($query_i < $query_num) {
151 $query_is = $query_i+1;
152 $query_val[$query_is] = $query_vars[$query_i];
153 ++$query_i; }
154 $query_val[0] = $query_string;
155 return call_user_func_array("sprintf",$query_val); }
156 function sql_set_charset($charset,$link=null) {
157 if(function_exists('mysql_set_charset')===false) {
158 if(!isset($link)) {
159 $result = sql_query("SET CHARACTER SET '".$charset."'"); }
160 if(isset($link)) {
161 $result = sql_query("SET CHARACTER SET '".$charset."'",$link); }
162 if ($result===false) {
163 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
164 return false; }
165 if(!isset($link)) {
166 $result = sql_query("SET NAMES '".$charset."'"); }
167 if(isset($link)) {
168 $result = sql_query("SET NAMES '".$charset."'",$link); }
169 if ($result===false) {
170 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
171 return false; }
172 return true; }
173 if(function_exists('mysql_set_charset')===true) {
174 if(isset($link)) {
175 $result = mysql_set_charset($charset,$link); }
176 if(!isset($link)) {
177 $result = mysql_set_charset($charset); }
178 if ($result===false) {
179 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
180 return false; }
181 return true; } }
183 if(function_exists('mysql_set_charset')===false) {
184 function mysql_set_charset($charset,$link) {
185 if(isset($link)) {
186 $result = sql_set_charset($charset,$link); }
187 if(!isset($link)) {
188 $result = sql_set_charset($charset); }
189 if ($result===false) {
190 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
191 return false; }
192 return true; } }
194 // Get next id for stuff
195 function sql_get_next_id($tablepre,$table,$link=null) {
196 $nid = mysql_insert_id($link);
197 return $nid; }
198 // Get number of rows for table
199 function sql_get_num_rows($tablepre,$table,$link=null) {
200 $getnextidq = sql_pre_query("SHOW TABLE STATUS LIKE '".$tablepre.$table."'", array());
201 if(!isset($link)) {
202 $getnextidr = sql_query($getnextidq); }
203 if(isset($link)) {
204 $getnextidr = sql_query($getnextidq,$link); }
205 $getnextid = sql_fetch_assoc($getnextidr);
206 return $getnextid['Rows'];
207 @sql_free_result($getnextidr); }