PHP 连接MySQL的几种方法
方法1: mysql_connect
# mysql $host = '11.22.27.138'; $user = 'ithomer'; $pass = 'yanggang'; $dbname = 'proxy_ithomer_net'; $query = "select * from proxy_all limit 0,10;"; $link = mysql_connect($host, $user, $pass); mysql_select_db($dbname); $result = mysql_query($query); while($line = mysql_fetch_array($result, MYSQL_ASSOC)){ print($line["ip"].":"); print($line["port"]."</br>"); }
方法2: mysqli
<?php $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db'); if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } if (mysqli_connect_error()) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } echo 'Success... ' . $mysqli->host_info . "\n"; $mysqli->close(); ?>
或
# mysqli $host = '11.22.27.138'; $user = 'ithomer'; $pass = 'yanggang'; $dbname = 'proxy_ithomer_net'; $query = "select * from proxy_all limit 0,10;"; $mysqli = new mysqli(); $mysqli->connect($host, $user, $pass, $dbname, 3306); print("++++++mysqlli info".$mysqli->host_info."</br>"); $result = $mysqli->query($query); while($line = mysqli_fetch_assoc($result)){ print($line["ip"].":"); print($line["port"]."</br>"); } $mysqli->close();
方法3:PDO
PDO: $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); MYSQLI: $link = mysqli_connect( 'localhost', /* The host to connect to 连接MySQL地址 */ 'user', /* The user to connect as 连接MySQL用户名 */ 'password', /* The password to use 连接MySQL密码 */ 'world'); /* The default database to query 连接数据库名称*/ if (!$link) { printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error()); exit; }
或
# pdo_mysql $host = '11.22.27.138'; $user = 'ithomer'; $pass = 'yanggang'; $dbname = 'proxy_ithomer_net'; $query = "select * from proxy_all limit 0,10;"; $dsn = sprintf("mysql:host=%s;dbname=%s", $host, $dbname); print($dsn."</br>"); $pdo = new PDO($dsn, $user, $pass); $result = $pdo->query($query); while($row = $result->fetch()) { print_r($row["ip"].":"); print_r($row["port"]."</br>"); }
解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
php 5个版本,5.2、5.3、5.4、5.5,怕跟不上时代,新的服务器直接上5.5,但是程序出现如下错误:Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in,看意思就很明了,说mysql_connect这个模块将在未来弃用,请你使用mysqli或者PDO来替代。
解决方法1:
禁止php报错
display_errors = On 改为 display_errors = Off
鉴于这个服务器都是给用户用的,有时候他们需要报错(…都是给朋友用的),不能这做,让他们改程序吧,看方案2.
解决方法2:
常用的php语法连接mysql如下
<?php $link = mysql_connect('localhost', 'user', 'password'); mysql_select_db('dbname', $link); 改成mysqi <?php $link = mysqli_connect('localhost', 'user', 'password', 'dbname');
常用mysql建表SQL如下
<?php // 老的 mysql_query('CREATE TEMPORARY TABLE `table`', $link); // 新的 mysqli_query($link, 'CREATE TEMPORARY TABLE `table`');
解决方法三:
在php程序代码里面设置报警级别
<?php error_reporting(E_ALL ^ E_DEPRECATED);
Deprecated的问题就这样解决掉了,不过还是建议大家尽快取消mysql的用户,全部都走向mysqli或者mysqlnd等等。mysql确实是太不安全而且太老旧了。
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2014-08-17 15:27:00
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!
转载注明: PHP 连接MySQL的几种方法 (米扑博客)