OSDN Git Service

10032660d950155c1d4abc7a991ebe9796c68916
[idb/iDB.git.git] / inc / misc / sql / sqlite3.php
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.
5
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.
10
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
13
14     $FileInfo: sqlite3.php - Last Update: 4/8/2022 SVN 947 - Author: cooldude2k $
15 */
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  if(isset($string)) {
128         $string = SQLite3::escapeString($string); }
129 if ($string===false) {
130     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
131         return false; }
132         return $string; }
133 // SafeSQL Lite Source Code by Cool Dude 2k
134 // Make SQL Query's safe
135 function sql_pre_query($query_string,$query_vars) {
136    $query_array = array(array("%i","%I","%F","%S"),array("%d","%d","%f","%s"));
137    $query_string = str_replace($query_array[0], $query_array[1], $query_string);
138    if (get_magic_quotes_gpc()) {
139        $query_vars  = array_map("stripslashes", $query_vars); }
140    $query_vars = array_map("sql_escape_string", $query_vars);
141    $query_val = $query_vars;
142 $query_num = count($query_val);
143 $query_i = 0;
144 while ($query_i < $query_num) {
145 $query_is = $query_i+1;
146 $query_val[$query_is] = $query_vars[$query_i];
147 ++$query_i; }
148    $query_val[0] = $query_string;
149    return call_user_func_array("sprintf",$query_val); }
150 function sql_set_charset($charset,$link=null) {
151         return true; }
152 /*
153 function sql_set_charset($charset,$link=null) {
154 if(function_exists('mysql_set_charset')===false) {
155 if(!isset($link)) {
156         $result = sql_query("SET CHARACTER SET '".$charset."'"); }
157 if(isset($link)) {
158         $result = sql_query("SET CHARACTER SET '".$charset."'",$link); }
159 if ($result===false) {
160     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
161         return false; }
162 if(!isset($link)) {
163         $result = sql_query("SET NAMES '".$charset."'"); }
164 if(isset($link)) {
165         $result = sql_query("SET NAMES '".$charset."'",$link); } 
166 if ($result===false) {
167     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
168         return false; }
169         return true; }
170 if(function_exists('mysql_set_charset')===true) {
171 if(isset($link)) {
172         $result = mysql_set_charset($charset,$link); }
173 if(!isset($link)) {
174         $result = mysql_set_charset($charset); }
175 if ($result===false) {
176     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
177         return false; }
178         return true; }
179 if(function_exists('mysql_set_charset')===false) {
180 function mysql_set_charset($charset,$link) {
181 if(isset($link)) {
182         $result = sql_set_charset($charset,$link); }
183 if(!isset($link)) {
184         $result = sql_set_charset($charset); }
185 if ($result===false) {
186     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
187         return false; }
188         return true; } }
189 */
190 // Get next id for stuff
191 function sql_get_next_id($tablepre,$table,$link=null) {
192         if(isset($link)) {
193                 $nid = $link->lastInsertRowID(); }
194         if(!isset($link)) {
195                 $nid = $SQLStat->lastInsertRowID(); }
196         return $nid; }
197 // Get number of rows for table
198 function sql_get_num_rows($tablepre,$table,$link=null) {
199    $getnextidq = sql_pre_query("SHOW TABLE STATUS LIKE '".$tablepre.$table."'", array());
200 if(!isset($link)) {
201         $getnextidr = sql_query($getnextidq); }
202 if(isset($link)) {
203         $getnextidr = sql_query($getnextidq,$link); } 
204    $getnextid = sql_fetch_assoc($getnextidr);
205    return $getnextid['Rows'];
206    @sql_free_result($getnextidr); }
207 ?>