【发布时间】:2017-10-11 09:41:45
【问题描述】:
我正在尝试连接到我的数据库,但它在 mysql_connect 函数中显示错误。
错误是: 致命错误:未捕获错误:调用 C:\xampp\htdocs\Connect.php:12 中未定义的函数 mysql_connect() 堆栈跟踪:#0 C:\xampp\htdocs\Test.php(3): require() #1 {main} 在第 12 行的 C:\xampp\htdocs\Connect.php 中抛出
连接文件:
<?php
$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "root";
// Place the password for the MySQL database here
$db_pass = "";
// Place the name for the MySQL database here
$db_name = "oscar";
// Run the connection here
$con = mysql_connect("db_host","$db_username","$db_pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name", $con);
try
{
$conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_username, $db_pass);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
文本文件:
<?php
// Connect to the MySQL database
require "Connect.php";
echo "Success";
?>
【问题讨论】:
-
PHP 是什么版本的?你知道
mysql函数已被弃用并从 PHP 7+ 中移除? -
你的 php 版本是多少。 mysql_connect 这个扩展在 PHP 5.5.0 中被弃用,在 PHP 7.0.0 中被移除
-
这一行是错误的
$con = mysql_connect("db_host","$db_username","$db_pass"); -
那么这段代码永远不会工作@Pixy_vxxc 你必须使用 PDO 或 mysqli 查询
-
如果你可以升级到 PHP7,那么也升级到 MYSQLI 或 PDO
标签: php mysql mysql-connect