-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_sqlite.php
102 lines (78 loc) · 3 KB
/
db_sqlite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
// Needs SQLite3: install using the correct `php --version`
// sudo apt-get install php7.4-sqlite
define( "MYSQL_DB", "freeswitch_cidlookup" );
if ( !class_exists( "SQLite3", TRUE ) )
{
echo "SQLite ERROR";
exit;
}
function dbLookup( $_arrNumberInfo )
{
global $debug;
// TODO: handle file exception
if (!$db = new SQLite3( MYSQL_DB.".db" ))
{
//if ( !isset( $debug ))
echo "could not connect to database";
return false;
}
// TODO: close db
// Partial number full listing
//$stmt = $db->prepare("SELECT name FROM telephone_names WHERE country_code=:country AND number LIKE ':international%' ORDER BY LENGTH(number), sortorder LIMIT 1");
//$stmt->bindParam(':country', $_arrNumberInfo['country'], SQLITE3_TEXT);
//$stmt->bindParam(':international', $_arrNumberInfo['international'], SQLITE3_TEXT);
// Full number partial listing (experimental)
$stmt = $db->prepare("SELECT name FROM telephone_names WHERE country_code=:country AND INSTR( ':international', number ) = 1 ORDER BY LENGTH(number) LIMIT 1");
if ( !$stmt )
{
$db->query("CREATE TABLE telephone_names (name TEXT, country_code TEXT, number TEXT)");
$stmt = $db->prepare("SELECT name FROM telephone_names WHERE country_code=:country AND INSTR( ':international', number ) = 1 ORDER BY LENGTH(number) LIMIT 1");
if ( !$stmt )
{
echo "table create error";
return false;
}
}
$stmt->bindParam(':country', $_arrNumberInfo['country'], SQLITE3_TEXT);
$stmt->bindParam(':international', $_arrNumberInfo['international'], SQLITE3_TEXT);
// Full number full listing
//$stmt = $db->prepare("SELECT name FROM telephone_names WHERE country_code=:country AND number = ':international' ORDER BY LENGTH(number), sortorder LIMIT 1");
//$stmt->bindParam(':country', $_arrNumberInfo['country'], SQLITE3_TEXT);
//$stmt->bindParam(':international', $_arrNumberInfo['international'], SQLITE3_TEXT);
if (!$result = $stmt->execute())
return false;
$row = $result->fetchArray( SQLITE3_ASSOC );
return $row[0];
}
function dbInsert( $_arrNumberInfo )
{
/*
international
national
local
type
*/
// TODO
echo "dbInsert";
var_dump( $_arrNumberInfo );
return false;
echo "DB insert";
if (!$db = new SQLite3( MYSQL_DB.".db" ))
return false;
// TODO: close db
// Prevent SQL injection on variables
$country = mysql_real_escape_string( $_arrNumberInfo['country'] );
$international = mysql_real_escape_string( $_arrNumberInfo['international'] );
$name = mysql_real_escape_string( $_arrNumberInfo['name'] );
$query = "INSERT INTO telephone_names (country_code,number,name) VALUES (".$country.",'".$international."','".$name."')";
if (!$result = mysql_query( $query, $db ))
{
echo mysql_error( $db );
return false;
}
//print_r( $_arrNumberInfo );
mysql_close( $db );
return true;
}
?>