php连接mysql
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php连接mysql相关的知识,希望对你有一定的参考价值。
<?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); }
以上是关于php连接mysql的主要内容,如果未能解决你的问题,请参考以下文章