OSDN Git Service

5ef34276e3c3bf5f1a75b3979a79e240e3fb11da
[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-2017 iDB Support - http://idb.berlios.de/
12     Copyright 2004-2017 Game Maker 2k - http://gamemaker2k.org/
13
14     $FileInfo: sqlite3.php - Last Update: 01/26/2017 SVN 810 - 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 while ($num<$row) {
94         $result->fetchArray();
95     $num++; }
96 if ($check===false) {
97     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
98         return false; }
99 $trow = $result->fetchArray();
100 if(!isset($trow[$field])) { $trow[$field] = null; }
101 $retval = $trow[$field]; 
102 return $retval; }
103 // Free Results :P
104 function sql_free_result($result) {
105         return true; }
106 //Fetch Results to Array
107 function sql_fetch_array($result,$result_type=SQLITE3_BOTH) {
108 $row = $result->fetchArray($result_type);
109         return $row; }
110 //Fetch Results to Associative Array
111 function sql_fetch_assoc($result) {
112 $row = $result->fetchArray(SQLITE3_ASSOC);
113         return $row; }
114 //Fetch Row Results
115 function sql_fetch_row($result) {
116 $row = $result->fetchArray(SQLITE3_NUM);
117         return $row; }
118 //Get Server Info
119 function sql_server_info($link=null) {
120         $result = SQLite3::version()['versionString'];
121         return $result; }
122 //Get Client Info
123 function sql_client_info($link=null) {
124         return null; }
125 function sql_escape_string($string,$link=null) {
126         $string = SQLite3::escapeString($string);
127 if ($string===false) {
128     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
129         return false; }
130         return $string; }
131 // SafeSQL Lite Source Code by Cool Dude 2k
132 // Make SQL Query's safe
133 function sql_pre_query($query_string,$query_vars) {
134    $query_array = array(array("%i","%I","%F","%S"),array("%d","%d","%f","%s"));
135    $query_string = str_replace($query_array[0], $query_array[1], $query_string);
136    if (get_magic_quotes_gpc()) {
137        $query_vars  = array_map("stripslashes", $query_vars); }
138    $query_vars = array_map("sql_escape_string", $query_vars);
139    $query_val = $query_vars;
140 $query_num = count($query_val);
141 $query_i = 0;
142 while ($query_i < $query_num) {
143 $query_is = $query_i+1;
144 $query_val[$query_is] = $query_vars[$query_i];
145 ++$query_i; }
146    $query_val[0] = $query_string;
147    return call_user_func_array("sprintf",$query_val); }
148 function sql_set_charset($charset,$link=null) {
149         return true; }
150 /*
151 function sql_set_charset($charset,$link=null) {
152 if(function_exists('mysql_set_charset')===false) {
153 if(!isset($link)) {
154         $result = sql_query("SET CHARACTER SET '".$charset."'"); }
155 if(isset($link)) {
156         $result = sql_query("SET CHARACTER SET '".$charset."'",$link); }
157 if ($result===false) {
158     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
159         return false; }
160 if(!isset($link)) {
161         $result = sql_query("SET NAMES '".$charset."'"); }
162 if(isset($link)) {
163         $result = sql_query("SET NAMES '".$charset."'",$link); } 
164 if ($result===false) {
165     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
166         return false; }
167         return true; }
168 if(function_exists('mysql_set_charset')===true) {
169 if(isset($link)) {
170         $result = mysql_set_charset($charset,$link); }
171 if(!isset($link)) {
172         $result = mysql_set_charset($charset); }
173 if ($result===false) {
174     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
175         return false; }
176         return true; }
177 if(function_exists('mysql_set_charset')===false) {
178 function mysql_set_charset($charset,$link) {
179 if(isset($link)) {
180         $result = sql_set_charset($charset,$link); }
181 if(!isset($link)) {
182         $result = sql_set_charset($charset); }
183 if ($result===false) {
184     output_error("SQL Error: ".sql_error(),E_USER_ERROR);
185         return false; }
186         return true; } }
187 */
188 // Get next id for stuff
189 function sql_get_next_id($tablepre,$table,$link=null) {
190         if(isset($link)) {
191                 $nid = $link->lastInsertRowID(); }
192         if(!isset($link)) {
193                 $nid = $SQLStat->lastInsertRowID(); }
194         return $nid; }
195 // Get number of rows for table
196 function sql_get_num_rows($tablepre,$table,$link=null) {
197    $getnextidq = sql_pre_query("SHOW TABLE STATUS LIKE '".$tablepre.$table."'", array());
198 if(!isset($link)) {
199         $getnextidr = sql_query($getnextidq); }
200 if(isset($link)) {
201         $getnextidr = sql_query($getnextidq,$link); } 
202    $getnextid = sql_fetch_assoc($getnextidr);
203    return $getnextid['Rows'];
204    @sql_free_result($getnextidr); }
205 ?>