-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_mysql.php
95 lines (67 loc) · 2.55 KB
/
db_mysql.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
<?php
define( "MYSQL_DB", "freeswitch_cidlookup" );
if ( !function_exists( "mysql_connect" ))
{
echo "MySQL ERROR";
exit;
}
function dbLookup( $_arrNumberInfo )
{
global $debug;
if (!$db = mysql_connect( NULL, "freeswitch", "frees!witch" ))
{
//if ( !isset( $debug ))
echo "could not connect to database";
return false;
}
// TODO: close db
if (!mysql_select_db( MYSQL_DB, $db ))
return false;
// Prevent SQL injection on variables
$country = mysql_real_escape_string( $_arrNumberInfo['country'] );
$international = mysql_real_escape_string( $_arrNumberInfo['international'] );
// Partial number full listing
//$query = "SELECT name FROM telephone_names WHERE country_code=".$country." AND number LIKE '".$international."%' ORDER BY LENGTH(number), sortorder LIMIT 1";
// Full number partial listing (experimental)
//$query = "SELECT name FROM telephone_names WHERE country_code=".$_arrNumberInfo['country']." AND number LIKE SUBSTR('".$_arrNumberInfo['international']."',0,LENGTH(NUMBER)) ORDER BY LENGTH(number), sortorder LIMIT 1";
$query = "SELECT name FROM telephone_names WHERE country_code=".$country." AND INSTR( '".$international."', number ) = 1 ORDER BY LENGTH(number), sortorder LIMIT 1";
// Full number full listing
//$query = "SELECT name FROM telephone_names WHERE country_code=".$country." AND number = '".$international."' ORDER BY LENGTH(number), sortorder LIMIT 1";
if (!$result = mysql_query( $query, $db ))
return false;
$row = mysql_fetch_row( $result );
mysql_close( $db );
//print_r( $row );
//, );
//print_r( $_arrNumberInfo );
return $row[0];
}
function dbInsert( $_arrNumberInfo )
{
/*
international
national
local
type
*/
echo "DB insert";
if (!$db = mysql_connect( NULL, "freeswitch", "frees!witch" ))
return false;
// TODO: close db
if (!mysql_select_db( "freeswitch_cidlookup", $db ))
return false;
// 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;
}
?>