Small update
[iDB.git] / inc / misc / sql / sqlite3.php
blobc24fa7213c1530f03ed39de036678f8b333282f4
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: sqlite3.php - Last Update: 08/02/2019 SVN 905 - Author: cooldude2k $
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="sqlite3.php"||$File3Name=="/sqlite3.php") {
18 @header('Location: index.php');
19 exit(); }
20 // SQLite Functions.
21 function sql_error($link=null) {
22 global $SQLStat;
23 if(isset($link)) {
24 $result = $link->lastErrorMsg(); }
25 if(!isset($link)) {
26 $result = $SQLStat->lastErrorMsg(); }
27 if ($result=="") {
28 return ""; }
29 return $result; }
30 function sql_errno($link=null) {
31 global $SQLStat;
32 if(isset($link)) {
33 $result = $link->lastErrorCode(); }
34 if(!isset($link)) {
35 $result = $SQLStat->lastErrorCode(); }
36 if ($result===0) {
37 return 0; }
38 return $result; }
39 function sql_errorno($link=null) {
40 global $SQLStat;
41 if(isset($link)) {
42 $result = $link->lastErrorCode().": ".$link->lastErrorMsg(); }
43 if(!isset($link)) {
44 $result = $SQLStat->lastErrorCode().": ".$SQLStat->lastErrorMsg(); }
45 if ($result=="") {
46 return ""; }
47 return $result; }
48 // Execute a query :P
49 $NumQueries = 0;
50 function sql_query($query,$link=null) {
51 global $NumQueries,$SQLStat;
52 if(isset($link)) {
53 $result = $link->query($query); }
54 if(!isset($link)) {
55 $result = $SQLStat->query($query); }
56 if ($result===false) {
57 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
58 return false; }
59 if ($result!==false) {
60 ++$NumQueries;
61 return $result; } }
62 //Fetch Number of Rows
63 function sql_num_rows($result) {
64 $num = 0;
65 $result->reset();
66 while ($result->fetchArray()) {
67 $num++; }
68 $result->reset();
69 if ($num===false) {
70 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
71 return false; }
72 return $num; }
73 // Connect to sqlite database
74 function sql_connect_db($server,$username,$password,$database=null,$new_link=false) {
75 if($new_link!==true) { $new_link = false; }
76 if($database===null) {
77 return true; }
78 if($database!==null) {
79 $link = new SQLite3($database,SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE); }
80 if ($link===false) {
81 output_error("Not connected: ".$sqliteerror,E_USER_ERROR);
82 return false; }
83 return $link; }
84 function sql_disconnect_db($link=null) {
85 if(isset($link)) {
86 return $link->close(); }
87 if(!isset($link)) {
88 return $SQLStat->close(); } }
89 // Query Results :P
90 function sql_result($result,$row,$field=0) {
91 $check = true;
92 $num = 0;
93 $result->reset();
94 while ($num<$row) {
95 $result->fetchArray();
96 $num++; }
97 if ($check===false) {
98 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
99 return false; }
100 $trow = $result->fetchArray();
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 return true; }
107 //Fetch Results to Array
108 function sql_fetch_array($result,$result_type=SQLITE3_BOTH) {
109 $row = $result->fetchArray($result_type);
110 return $row; }
111 //Fetch Results to Associative Array
112 function sql_fetch_assoc($result) {
113 $row = $result->fetchArray(SQLITE3_ASSOC);
114 return $row; }
115 //Fetch Row Results
116 function sql_fetch_row($result) {
117 $row = $result->fetchArray(SQLITE3_NUM);
118 return $row; }
119 //Get Server Info
120 function sql_server_info($link=null) {
121 $result = SQLite3::version()['versionString'];
122 return $result; }
123 //Get Client Info
124 function sql_client_info($link=null) {
125 return null; }
126 function sql_escape_string($string,$link=null) {
127 $string = SQLite3::escapeString($string);
128 if ($string===false) {
129 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
130 return false; }
131 return $string; }
132 // SafeSQL Lite Source Code by Cool Dude 2k
133 // Make SQL Query's safe
134 function sql_pre_query($query_string,$query_vars) {
135 $query_array = array(array("%i","%I","%F","%S"),array("%d","%d","%f","%s"));
136 $query_string = str_replace($query_array[0], $query_array[1], $query_string);
137 if (get_magic_quotes_gpc()) {
138 $query_vars = array_map("stripslashes", $query_vars); }
139 $query_vars = array_map("sql_escape_string", $query_vars);
140 $query_val = $query_vars;
141 $query_num = count($query_val);
142 $query_i = 0;
143 while ($query_i < $query_num) {
144 $query_is = $query_i+1;
145 $query_val[$query_is] = $query_vars[$query_i];
146 ++$query_i; }
147 $query_val[0] = $query_string;
148 return call_user_func_array("sprintf",$query_val); }
149 function sql_set_charset($charset,$link=null) {
150 return true; }
152 function sql_set_charset($charset,$link=null) {
153 if(function_exists('mysql_set_charset')===false) {
154 if(!isset($link)) {
155 $result = sql_query("SET CHARACTER SET '".$charset."'"); }
156 if(isset($link)) {
157 $result = sql_query("SET CHARACTER SET '".$charset."'",$link); }
158 if ($result===false) {
159 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
160 return false; }
161 if(!isset($link)) {
162 $result = sql_query("SET NAMES '".$charset."'"); }
163 if(isset($link)) {
164 $result = sql_query("SET NAMES '".$charset."'",$link); }
165 if ($result===false) {
166 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
167 return false; }
168 return true; }
169 if(function_exists('mysql_set_charset')===true) {
170 if(isset($link)) {
171 $result = mysql_set_charset($charset,$link); }
172 if(!isset($link)) {
173 $result = mysql_set_charset($charset); }
174 if ($result===false) {
175 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
176 return false; }
177 return true; }
178 if(function_exists('mysql_set_charset')===false) {
179 function mysql_set_charset($charset,$link) {
180 if(isset($link)) {
181 $result = sql_set_charset($charset,$link); }
182 if(!isset($link)) {
183 $result = sql_set_charset($charset); }
184 if ($result===false) {
185 output_error("SQL Error: ".sql_error(),E_USER_ERROR);
186 return false; }
187 return true; } }
189 // Get next id for stuff
190 function sql_get_next_id($tablepre,$table,$link=null) {
191 if(isset($link)) {
192 $nid = $link->lastInsertRowID(); }
193 if(!isset($link)) {
194 $nid = $SQLStat->lastInsertRowID(); }
195 return $nid; }
196 // Get number of rows for table
197 function sql_get_num_rows($tablepre,$table,$link=null) {
198 $getnextidq = sql_pre_query("SHOW TABLE STATUS LIKE '".$tablepre.$table."'", array());
199 if(!isset($link)) {
200 $getnextidr = sql_query($getnextidq); }
201 if(isset($link)) {
202 $getnextidr = sql_query($getnextidq,$link); }
203 $getnextid = sql_fetch_assoc($getnextidr);
204 return $getnextid['Rows'];
205 @sql_free_result($getnextidr); }