如何用代码测试PHP连接MYSQL是否成功
展开全部
PHP连接mysql使用mysql_connect函数即可进行与mysql数据库之间的通讯。如果连接成功,则返回一个 MySQL 连接标识,失败则返回 FALSE。示例如下:<?php $host="localhost";//mysql<a href="https://www.baidu.com/s?wd=%E4%B8%BB%E6%9C%BA%E5%9C%B0%E5%9D%80&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y1mHcLnW9hm1fYrjfkPHnv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPH0vPHRYPjbk" target="_blank" class="baidu-highlight">主机地址</a>$user="root"; //mysql 登录账户$pwd=""; //mysql登录密码//连接数据库$conn = <a href="https://www.baidu.com/s?wd=mysql_connect&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y1mHcLnW9hm1fYrjfkPHnv0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPH0vPHRYPjbk" target="_blank" class="baidu-highlight">mysql_connect</a>($host,$user,$pwd);//判断if (!$conn) { die('连接数据库失败: ' . mysql_error()); }echo "mysql 连接成功!"; //其他代码...... // 关闭mysql连接mysql_close($conn);?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询