mysql_real_escape_stringを機能させるには、dbに接続する必要があることは理解していますが、その方法はよくわかりません。
この関数を使用して、テーブルに行を挿入します。
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;
}
}
}