1

我知道我需要连接到数据库才能使 mysql_real_escape_string 工作,但不太确定如何工作。

我使用此函数在表中插入行。

function insert_db($conn, $table_name, $array){

    //$array = array_map( 'mysql_real_escape_string' , $db ); //ERROR! 

    # Create the SQL Query
    $query = 'INSERT INTO `'.$table_name.'` '.
             '( `'.implode( '` , `' , array_keys( $array ) ).'` ) '.
             'VALUES '.
             '( "'.implode( '" , "' , $array ).'" )';

    $result = $conn->query($query);             
    if (!$result){ trigger_error("mysql error: ".mysql_errno($result) . ": " . mysql_error($result));  }
}

例如,它需要一个这样的数组:

$db["to"] = $to;
$db["from"] = $username; 
$db["message"] = $message;
$db["time"] = date("Y-m-d H:i:s", strtotime("+0 minutes"));

insert_db($conn, "inbox", $db)

其中数组键表示表中的列。

但我得到这个错误:

2011-02-01 22:21:29 : mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) 

有人问从哪里来$conn

$conn = db_connect();

if( ! function_exists('db_connect')){

    function db_connect() {
    $result = new mysqli('localhost', 'xxx', 'xxx', 'xxx');
    if (!$result) {
    die(msg(0,"Could not connect to database server"));
    } else {
    return $result;
    }
    }

}
4

1 回答 1

5

您的连接正在使用 mysqli ( i ),因此您不能使用 mysql* 函数,您需要 mysqli_real_escape_string

于 2011-02-01T23:46:50.310 回答