gan*_*jan 1 php mysql mysql-error-1045
我知道我需要连接到db才能使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)); }
}
Run Code Online (Sandbox Code Playgroud)
它需要一个这样的数组,例如:
$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)
Run Code Online (Sandbox Code Playgroud)
数组键表示表中的列.
但我得到这个错误:
2011-02-01 22:21:29 : mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO)
Run Code Online (Sandbox Code Playgroud)
有人问到哪里$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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4425 次 |
| 最近记录: |