mysqli 작업 중
This commit is contained in:
@ -1416,11 +1416,22 @@ function html_symbol($str)
|
||||
*************************************************************************/
|
||||
|
||||
// DB 연결
|
||||
function sql_connect($host, $user, $pass)
|
||||
function sql_connect($host, $user, $pass, $db=G5_MYSQL_DB)
|
||||
{
|
||||
global $g5;
|
||||
|
||||
return @mysql_connect($host, $user, $pass);
|
||||
if(function_exists('mysqli_connect')) {
|
||||
$link = mysqli_connect($host, $user, $pass, $db);
|
||||
|
||||
// 연결 오류 발생 시 스크립트 종료
|
||||
if (mysqli_connect_errno()) {
|
||||
die('Connect Error: '.mysqli_connect_error());
|
||||
}
|
||||
} else {
|
||||
$link = mysql_connect($host, $user, $pass);
|
||||
}
|
||||
|
||||
return $link;
|
||||
}
|
||||
|
||||
|
||||
@ -1429,16 +1440,36 @@ function sql_select_db($db, $connect)
|
||||
{
|
||||
global $g5;
|
||||
|
||||
return @mysql_select_db($db, $connect);
|
||||
if(function_exists('mysqli_select_db'))
|
||||
return @mysqli_select_db($connect, $db);
|
||||
else
|
||||
return @mysql_select_db($db, $connect);
|
||||
}
|
||||
|
||||
|
||||
// mysql_query 와 mysql_error 를 한꺼번에 처리
|
||||
// mysql connect resource 지정 - 명랑폐인님 제안
|
||||
function sql_query($sql, $error=G5_DISPLAY_SQL_ERROR)
|
||||
function sql_set_charset($charset, $link=null)
|
||||
{
|
||||
global $g5;
|
||||
|
||||
if(!$link)
|
||||
$link = $g5['connect_db'];
|
||||
|
||||
if(function_exists('mysqli_set_charset'))
|
||||
mysqli_set_charset($link, $charset);
|
||||
else
|
||||
sql_query(" set names {$charset} ");
|
||||
}
|
||||
|
||||
|
||||
// mysqli_query 와 mysqli_error 를 한꺼번에 처리
|
||||
// mysql connect resource 지정 - 명랑폐인님 제안
|
||||
function sql_query($sql, $error=G5_DISPLAY_SQL_ERROR, $link=null)
|
||||
{
|
||||
global $g5;
|
||||
|
||||
if(!$link)
|
||||
$link = $g5['connect_db'];
|
||||
|
||||
// Blind SQL Injection 취약점 해결
|
||||
$sql = trim($sql);
|
||||
// union의 사용을 허락하지 않습니다.
|
||||
@ -1447,20 +1478,34 @@ function sql_query($sql, $error=G5_DISPLAY_SQL_ERROR)
|
||||
// `information_schema` DB로의 접근을 허락하지 않습니다.
|
||||
$sql = preg_replace("#^select.*from.*where.*`?information_schema`?.*#i", "select 1", $sql);
|
||||
|
||||
if ($error)
|
||||
$result = @mysql_query($sql, $g5['connect_db']) or die("<p>$sql<p>" . mysql_errno() . " : " . mysql_error() . "<p>error file : {$_SERVER['SCRIPT_NAME']}");
|
||||
else
|
||||
$result = @mysql_query($sql, $g5['connect_db']);
|
||||
if(function_exists('mysqli_query')) {
|
||||
if ($error) {
|
||||
$result = @mysqli_query($link, $sql) or die("<p>$sql<p>" . mysqli_errno($link) . " : " . mysqli_error($link) . "<p>error file : {$_SERVER['SCRIPT_NAME']}");
|
||||
} else {
|
||||
$result = @mysqli_query($link, $sql);
|
||||
}
|
||||
} else {
|
||||
if ($error) {
|
||||
$result = @mysql_query($sql, $link) or die("<p>$sql<p>" . mysql_errno() . " : " . mysql_error() . "<p>error file : {$_SERVER['SCRIPT_NAME']}");
|
||||
} else {
|
||||
$result = @mysql_query($sql, $link);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
// 쿼리를 실행한 후 결과값에서 한행을 얻는다.
|
||||
function sql_fetch($sql, $error=G5_DISPLAY_SQL_ERROR)
|
||||
function sql_fetch($sql, $error=G5_DISPLAY_SQL_ERROR, $link=null)
|
||||
{
|
||||
$result = sql_query($sql, $error);
|
||||
//$row = @sql_fetch_array($result) or die("<p>$sql<p>" . mysql_errno() . " : " . mysql_error() . "<p>error file : $_SERVER['SCRIPT_NAME']");
|
||||
global $g5;
|
||||
|
||||
if(!$link)
|
||||
$link = $g5['connect_db'];
|
||||
|
||||
$result = sql_query($sql, $error, $link);
|
||||
//$row = @sql_fetch_array($result) or die("<p>$sql<p>" . mysqli_errno() . " : " . mysqli_error() . "<p>error file : $_SERVER['SCRIPT_NAME']");
|
||||
$row = sql_fetch_array($result);
|
||||
return $row;
|
||||
}
|
||||
@ -1469,7 +1514,11 @@ function sql_fetch($sql, $error=G5_DISPLAY_SQL_ERROR)
|
||||
// 결과값에서 한행 연관배열(이름으로)로 얻는다.
|
||||
function sql_fetch_array($result)
|
||||
{
|
||||
$row = @mysql_fetch_assoc($result);
|
||||
if(function_exists('mysqli_fetch_assoc'))
|
||||
$row = @mysqli_fetch_assoc($result);
|
||||
else
|
||||
$row = @mysql_fetch_assoc($result);
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
@ -1479,7 +1528,10 @@ function sql_fetch_array($result)
|
||||
// 단, 결과 값은 스크립트(script) 실행부가 종료되면서 메모리에서 자동적으로 지워진다.
|
||||
function sql_free_result($result)
|
||||
{
|
||||
return mysql_free_result($result);
|
||||
if(function_exists('mysqli_free_result'))
|
||||
return mysqli_free_result($result);
|
||||
else
|
||||
return mysql_free_result($result);
|
||||
}
|
||||
|
||||
|
||||
@ -1493,6 +1545,59 @@ function sql_password($value)
|
||||
}
|
||||
|
||||
|
||||
function sql_insert_id($link=null)
|
||||
{
|
||||
global $g5;
|
||||
|
||||
if(!$link)
|
||||
$link = $g5['connect_db'];
|
||||
|
||||
if(function_exists('mysqli_insert_id'))
|
||||
return mysqli_insert_id($link);
|
||||
else
|
||||
return mysql_insert_id($link);
|
||||
}
|
||||
|
||||
|
||||
function sql_num_rows($result)
|
||||
{
|
||||
if(function_exists('mysqli_num_rows'))
|
||||
return mysqli_num_rows($result);
|
||||
else
|
||||
return mysql_num_rows($result);
|
||||
}
|
||||
|
||||
|
||||
function sql_field_names($table, $link=null)
|
||||
{
|
||||
global $g5;
|
||||
|
||||
if(!$link)
|
||||
$link = $g5['connect_db'];
|
||||
|
||||
$columns = array();
|
||||
|
||||
$sql = " select * from `$table` limit 1 ";
|
||||
$result = sql_query($sql, $link);
|
||||
|
||||
if(function_exists('mysqli_fetch_field')) {
|
||||
while($field = mysqli_fetch_field($result)) {
|
||||
$columns[] = $field->name;
|
||||
}
|
||||
} else {
|
||||
$i = 0;
|
||||
$cnt = mysql_num_fields($result);
|
||||
while($i < $cnt) {
|
||||
$field = mysql_fetch_field($result, $i);
|
||||
$columns[] = $field->name;
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
|
||||
// PHPMyAdmin 참고
|
||||
function get_table_define($table, $crlf="\n")
|
||||
{
|
||||
@ -1893,12 +1998,15 @@ function convert_charset($from_charset, $to_charset, $str)
|
||||
}
|
||||
|
||||
|
||||
// mysql_real_escape_string 의 alias 기능을 한다.
|
||||
function sql_real_escape_string($field)
|
||||
// mysqli_real_escape_string 의 alias 기능을 한다.
|
||||
function sql_real_escape_string($str, $link=null)
|
||||
{
|
||||
global $g5;
|
||||
|
||||
return mysql_real_escape_string($field, $g5['connect_db']);
|
||||
if(!$link)
|
||||
$link = $g5['connect_db'];
|
||||
|
||||
return mysqli_real_escape_string($link, $str);
|
||||
}
|
||||
|
||||
function escape_trim($field)
|
||||
|
||||
Reference in New Issue
Block a user