【发布时间】:2013-03-05 09:57:16
【问题描述】:
我在连接数据库时遇到问题
这是我正在使用的代码
$con = mysql_connect('host', 'user', 'pass');
mysql_select_db('database_name', $con);
这是我得到的结果
警告:mysql_connect() [function.mysql-connect]:在“读取初始通信数据包”时丢失与 MySQL 服务器的连接,系统错误:/home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php 中的 111在第 2 行
警告:mysql_select_db() 期望参数 2 是资源,布尔值在第 3 行的 /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php 中给出
警告:mysql_connect() [function.mysql-connect]:在“读取初始通信数据包”时丢失与 MySQL 服务器的连接,系统错误:/home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php 中的 111在第 2 行
警告:mysql_select_db() 期望参数 2 是资源,布尔值在第 3 行的 /home/heartbeat_db/heartbeatsmart.com/php/config/dbconnect.php 中给出
【问题讨论】:
-
您的代码已被弃用。
-
不要使用mysql_connect 使用php.net/manual/en/mysqli.construct.php 或php.net/manual/en/pdo.construct.php
-
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial. -
我正准备转行,现在我有了更好的理由。新主机只接受mysqli。