从PHP5.5之后,会出现
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in D:\WEB\wwwroot\test.php on line 23
官网的changelong:
Version | Function | Description |
---|---|---|
5.5.0 | mysql_connect | This function will generate an E_DEPRECATED error. |
mysql_db_name | The mysql_list_dbs function is deprecated, and emits an E_DEPRECATED level error. | |
mysql_pconnect | This function will generate an E_DEPRECATED error. | |
mysql_tablename | The mysql_tablename function is deprecated, and emits an E_DEPRECATED level error. | |
5.3.0 | mysql_db_query | This function now throws an E_DEPRECATED notice. |
mysql_escape_string | This function now throws an E_DEPRECATED notice. | |
4.3.7 | mysql_list_tables | This function became deprecated. |
4.3.0 | mysql_escape_string | This function became deprecated, do not use this function. Instead, use mysql_real_escape_string. |
D:\progFiles\mariadb55\employees_db>mysql < employees.sql -u DBAdmin -p Enter password: ******* INFO CREATING DATABASE STRUCTURE INFO storage engine: InnoDB INFO LOADING departments INFO LOADING employees INFO LOADING dept_emp INFO LOADING dept_manager INFO LOADING titles INFO LOADING salaries D:\progFiles\mariadb55\employees_db>
以下为创建SQL的过程。
SELECT concat(first_name, ' ' , last_name) as name FROM `employees` WHERE emp_no = 10001
SELECT dept_no FROM `dept_emp` WHERE emp_no = 10001
SELECT dept_name FROM `departments` WHERE dept_no = (SELECT dept_no FROM `dept_emp` WHERE emp_no = 10001)
SELECT concat(e.first_name, ' ' , e.last_name) as name, d.dept_name FROM `employees` e, `departments` d WHERE d.dept_no = (SELECT dept_no FROM `dept_emp` WHERE emp_no = 10001) and e.emp_no = 10001
SELECT concat(e.first_name, ' ' , e.last_name) as name, d.dept_name, t.title FROM `employees` e, `departments` d, `titles` t WHERE d.dept_no = (SELECT dept_no FROM `dept_emp` WHERE emp_no = 10001) and e.emp_no = 10001 and t.emp_no = 10001
function conn() { //$con = mysql_pconnect("localhost","root","qazxsw"); $conn = mysql_connect("localhost", "DBAdmin", "xbfirst"); //$conn = mysql_connect("localhost","root","xbfirst80"); //$conn = mysqli=_connect("localhost","tdtc2014","qazxsw"); if (!$conn) die('Could not connect: ' . mysql_error()); return $conn; }
mysql_select_db
(
database_name // 数据库名称
link_identifier // 链接ID
)
mysql_query
(
query // 查询语句
link_identifier // 链接ID
)
mysql_fetch_assoc
(
result // 结果集
)
function myQuery($sql, $conn) { if (!$conn) die('Could not connect: ' . mysql_error()); $db_selected = mysql_select_db("carnumber", $conn); $result = mysql_query($sql,$conn); while ($row = mysql_fetch_assoc($result)) { $rows[] = $row; } }
function conn_i() { $conn_i = mysqli_connect("localhost", "DBAdmin", "xbfirst"); if (!$conn_i) die('Could not connect: ' . mysqli_connect_errno()); return $conn_i; }
mysqli_select_db
(
link_identifier // 链接ID
database_name // 数据库名称
)
mysqli_query
(
link_identifier // 链接ID
query // 查询语句
)
mysqli_fetch_assoc
(
result // 结果集
)
function myQuery_i($sql_i, $conn_i) { if (!$conn_i) die('Could not connect: ' . mysqli_connect_errno()); $db_selected = mysqli_select_db($conn_i, "carnumber"); $result_i = mysqli_query($conn_i, $sql_i); while($row_i = mysqli_fetch_assoc($result_i)) { $rows_i[] = $row_i; } }
<?php phpinfo(); testQuery(); function testQuery() { $sql = "SELECT concat(e.first_name, ' ' , e.last_name) as name, d.dept_name as dept, t.title as title FROM `employees` e, `departments` d, `titles` t WHERE d.dept_no = (SELECT dept_no FROM `dept_emp` WHERE emp_no = 10001) and e.emp_no = 10001 and t.emp_no = 10001"; if (version_compare(PHP_VERSION, '5.5.0') >= 0) { $conn_i = conn_i(); myQuery_i($sql, $conn_i); closeConn_i($conn_i); } else { $conn = conn(); myQuery($sql, $conn); closeConn($conn); } } function conn() { //$con = mysql_pconnect("localhost","root","qazxsw"); $conn = mysql_connect("localhost", "DBAdmin", "xbfirst"); //$conn = mysql_connect("localhost","root","xbfirst80"); //$conn = mysql_connect("localhost","tdtc2014","qazxsw"); if (!$conn) die('Could not connect: ' . mysql_error()); return $conn; } function closeConn($conn) { mysql_close($conn); } function myQuery($sql, $conn) { if (!$conn) die('Could not connect: ' . mysql_error()); $db_selected = mysql_select_db("employees", $conn); $result = mysql_query($sql,$conn); while ($row = mysql_fetch_assoc($result)) { $rows[] = $row; } foreach ($rows as $r) { echo 'name: ' . $r['name'] . '</br>'; echo 'department: ' . $r['dept'] . '</br>'; echo 'title: ' . $r['title']; } } function conn_i() { $conn_i = mysqli_connect("localhost", "DBAdmin", "xbfirst"); if (!$conn_i) die('Could not connect: ' . mysqli_connect_errno()); return $conn_i; } function closeConn_i($conn_i) { mysqli_close($conn_i); } function myQuery_i($sql_i, $conn_i) { if (!$conn_i) die('Could not connect: ' . mysqli_connect_errno()); $db_selected = mysqli_select_db($conn_i, "employees"); $result_i = mysqli_query($conn_i, $sql_i); while($row_i = mysqli_fetch_assoc($result_i)) { $rows_i[] = $row_i; } foreach($rows_i as $row_i) { echo 'name: ' . $row_i['name'] . '</br>'; echo 'department: ' . $row_i['dept'] . '</br>'; echo 'title: ' . $row_i['title']; } /* free result set */ mysqli_free_result($result_i); } ?>
《MySQL vs. MySQLi: Is the Improvement Right for You?》
《MySQL vs MySQLi in PHP》
《MySQL 官方示例数据库安装》