php mysql连接得到错误信息
Posted
技术标签:
【中文标题】php mysql连接得到错误信息【英文标题】:php mysql connection get error message 【发布时间】:2015-07-28 03:50:33 【问题描述】:这是我的第一个 php 代码,我正在尝试使用用户名 = "root" 和密码 = "root" 连接到数据库
我有一个名为dbConnection.php
的连接文件
如下:
<?php
echo "in connection file";
$hostname = "localhost";
$username = "root";
$password = "root";
$db = "ledDB";
echo "<br> db-connection : vars definde ";
//connection to the database
$conn = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
echo "db-connection : initilize connection ";
mysql_select_db($db);
echo "db-connection : connection done";
// Check connection
echo "Connected successfully";
?>
我在一个名为 what.php 的文件中调用它:
<?php
echo "Hello";
include "dbConnection.php";
echo "ohhhh";
?>
这会返回状态码 500,这是内部服务器错误
但我想知道修复它的错误是什么我怎样才能得到错误消息?
我试过了
$conn = mysql_connect($hostname, $username, $password) or die("无法连接 MySQL");
但它没有返回任何东西。
谁能帮帮我?
【问题讨论】:
MySql 是否正在运行并验证登录凭据? 确保您的服务器正在运行。 使用命令行运行mysql检查用户名和密码 【参考方案1】:如果您在 localhost 上有密码,则使用密码,否则将其留空
$con = mysqli_connect("localhost","root","yourpassword","yourdb");
【讨论】:
【参考方案2】:你 mysqli_connect() 方法。
<?php
echo "in connection file";
$hostname = "localhost";
$username = "root";
$password = "root";
$db = "ledDB";
echo "<br> db-connection : vars definde ";
//connection to the database
$conn = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
echo "db-connection : initilize connection ";
mysqli_select_db($conn,$db);
echo "db-connection : connection done";
// Check connection
echo "Connected successfully";
?>
【讨论】:
【参考方案3】:第一件事是将连接方法更改为 mysqli 或我个人最喜欢的 PDO。 PDO 示例:
class db extends pdo
//Website Variables
public $sitedb = '';
public $siteconfig;
public $sitesettings = array(
'host' => 'localhost',
'database' => 'yourdb',
'username' => 'youruser',
'password' => 'yourpass',
);
public function __construct()
$this->sitedb = new PDO(
"mysql:host=$this->sitesettings['host'];" .
"dbname=$this->sitesettings['database'];" .
"charset=utf8",
"$this->sitesettings['username']",
"$this->sitesettings['password']"
);
$this->sitedb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db = new db();
然后您可以在包含 db.php 页面后将您的 PDO 类扩展到新的类。 示例选择:
class yourclass extends db
public function SelectUsers()
global $db;
$query = <<<SQL
SELECT email
FROM users
WHERE active = :active
SQL;
$resource = $db->sitedb->prepare( $query );
$resource->execute( array (
':active' => 1,
));
$count = $resource->rowCount();
foreach($resource as $user)
$this->email = $user['email'];
【讨论】:
【参考方案4】:<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn)
die("Connection failed: " . mysqli_connect_error());
echo "Connected successfully";
// make the current db
$db_selected = mysqli_select_db ( $conn , $dbname );
if (!$db_selected)
die ('Can\'t connect to Database : ' . mysql_error());
?>
【讨论】:
如果你可以在 localhost 中运行而不是 password="" 和 username ="root"以上是关于php mysql连接得到错误信息的主要内容,如果未能解决你的问题,请参考以下文章
用PHPstorm连接MySQL数据库,显示有错误(function my_connect deprecated)
php增加用户时出现的【SQLSTATE[HY000] [2002]