<?php
header("Content-type:text/html;Charset=utf-8");
$arr = array(
‘host‘ => ‘localhost‘,
‘port‘ => ‘3306‘,
‘user‘ => ‘root‘,
‘pwd‘ => ‘zmpan‘
);
function my_connect($arr){
$host = isset($arr[‘host‘]) ? $arr[‘host‘] : ‘localhsot‘;
$port = isset($arr[‘port‘]) ? $arr[‘port‘] : ‘3306‘;
$user = isset($arr[‘user‘]) ? $arr[‘user‘] : ‘root‘;
$pwd = isset($arr[‘pwd‘]) ? $arr[‘pwd‘] : ‘‘;
$link = @ mysql_connect("$host:$port",$user,$pwd);
if(!$link){
echo "SQL执行失败!<br />";
echo "编码错误:", mysql_errno(),‘<br />‘;
echo "错误信息:", mysql_error(), ‘<br />‘;
die;
}
}
function my_query($sql){
$result = mysql_query($sql);
if(!$result){
echo "SQL执行失败!<br />";
echo "编码错误:", mysql_errno(),‘<br />‘;
echo "错误信息:", mysql_error(), ‘<br />‘;
die;
}
return $result;
}
function my_charset($charset){
$sql = "set names $charset";
my_query($sql);
}
function my_database($db){
$sql = "use $db";
my_query($sql);
}
my_connect($arr);
my_charset(‘utf8‘);
my_database(‘homework‘);