求一段php连接mysql的代码
服务器 :localhost
端口:3307
用户名:root
密码:110
数据库:test
请尽量详细的说明下,我刚接触PHP,不大懂!学习下!谢谢了!:)
mysql安装的不是默然的3306 端口,是装在了3307上,所以连接需要端口才行! 请问带上端口的话,连接语句该怎么写啊?!写localhost 肯定不行了!localhost:3307 连接也是失败!
phpè¿æ¥mysqlèæ¬å¦ä¸ï¼
$host = 'mysqlæå¡å¨ipæåå';$port = 3307;//mysql端å£å·ï¼é»è®¤ä¸º3306ï¼æ¤å¤ä¸º3307
$user = 'mysqlç¨æ·å';
$pwd = 'å¯ç ';
$link = @mysql_connect("{$host}:{$port}", $user, $pwd, true);
if(!$link) {
die('Database Connect Error:'.mysql_error());
}
mysql_connectå½æ°ä»ç»ï¼
mysql_connect â æå¼ä¸ä¸ªå° MySQL æå¡å¨çè¿æ¥
resource mysql_connect ([ string $server [, string $username [, string $password [, bool $new_link [, int $client_flags ]]]]] )
æå¼æéå¤ä½¿ç¨ä¸ä¸ªå° MySQL æå¡å¨çè¿æ¥
serverï¼MySQL æå¡å¨ãå¯ä»¥å æ¬ç«¯å£å·ï¼ä¾å¦ "hostname:port"ï¼æè å°æ¬å°å¥æ¥åçè·¯å¾ï¼ä¾å¦å¯¹äº localhost ç ":/path/to/socket"ã
usernameï¼ç¨æ·åã
passwordï¼å¯ç ã
new_linkï¼4.2.0æ·»å ï¼ï¼å¦æç¨åæ ·çåæ°ç¬¬äºæ¬¡è°ç¨mysql_connect()ï¼å°ä¸ä¼å»ºç«æ°è¿æ¥ï¼èå°è¿åå·²ç»æå¼çè¿æ¥æ è¯ãåæ°new_link æ¹åæ¤è¡ä¸ºå¹¶ä½¿mysql_connect() æ»æ¯æå¼æ°çè¿æ¥ï¼çè³å½mysql_connect() æ¾å¨åé¢è¢«ç¨åæ ·çåæ°è°ç¨è¿ã
client_flagsï¼4.3.0æ·»å ï¼ï¼client_flagsåæ°å¯ä»¥æ¯ä»¥ä¸å¸¸éçç»åï¼MYSQL_CLIENT_SSLï¼MYSQL_CLIENT_COMPRESSï¼MYSQL_CLIENT_IGNORE_SPACEæMYSQL_CLIENT_INTERACTIVEã
è¿åå¼
å¦ææååè¿åä¸ä¸ª MySQL è¿æ¥æ è¯ï¼ æè å¨å¤±è´¥æ¶è¿å FALSEã
mysql_select_db("test",$link) or die("链接数据库失败");
echo "成功!";本回答被提问者采纳