php5以后推荐使用pdo或者mysqli连接数据库
<?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; try { $pdo = new PDO("mysql:host=$servername;dbname=myDb", $username, $password); echo "连接成功"; } catch(PDOException $e) { echo $e->getMessage(); } //$list = $pdo -> query("set names utf8"); $list = $pdo -> query("select * from myTable"); while($row = $list -> fetch()){ print_r($row); }
<?php $servername = "127.0.0.1"; $username = "root"; $password = "root"; // 创建连接 $mysqli = new mysqli($servername, $username, $password); // 检测连接 if ($mysqli->connect_error) { die("连接失败: " . $mysqli->connect_error); } $mysqli->select_db('three'); //$mysqli->query('set names utf8'); $list=$mysqli->query('select * from jing_province'); while($row=$list->fetch_row()){ print_r($row); }