2017-05-12 67 views
0

我想连接到我的数据库,但它显示我在mysql_connect函数中的错误。MySQL连接功能不起作用?

错误代码: 致命错误:未捕获错误:调用未定义的函数mysql_connect()在C:\ xampp \ htdocs \ Connect.php中:12堆栈跟踪:#0 C:\ xampp \ htdocs \ Test.php (3):要求()#1 {主}扔在C:\ XAMPP \ htdocs中\ Connect.php第12行

的连接文件:

<?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"; 
?> 
+2

它是什么版本的PHP?你知道'mysql'函数已被弃用并从PHP 7+中删除? – RamRaider

+0

什么是你的PHP版本。 mysql_connect 此扩展在PHP 5.5.0中被弃用,并且在PHP 7.0.0中被删除。 – ToujouAya

+0

这行错误:$ con = mysql_connect(“db_host”,“$ db_username”,“$ db_pass”);' – Akintunde007

回答

0

功能mysql_connect不赞成使用的功能。相反,你应该使用mysqli_connect了解更多关于这个here

下面的代码应该工作:

<?php 
/** 
* Created by PhpStorm. 
* User: ... 
* Date: 5-12-2017 
* Time: 09:47 
* Database connection. 
*/ 
?> 
<?php 
define('DB_SERVER', 'localhost'); 
define('DB_USERNAME', 'admin'); 
define('DB_PASSWORD', 'admin'); 
define('DB_DATABASE', 'your_database'); 
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); 

// Check connection 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
?> 
3

你为什么使用mysql_co同时甚至是PDO?并且mysql已被弃用,因此容易受到sql注入的影响。

只有这个代码将连接到数据库

<?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"; 


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(); 
} 
0

的MySQL函数在PHP 5.5.0被弃用。我改变了你的代码中的一些变化。 $符号被添加到mysqli连接语句中。

$con = mysqli_connect("$db_host","$db_username","$db_pass"); 
if (!$con) 
{ 
    die('Could not connect: ' . mysqli_error()); 
} 
mysqli_select_db("$db_name", $con); 
0

尝试使用mysqli

<?php 
$db_host = "localhost"; 
$db_username = "root"; // Place the username for the MySQL database here 
$db_pass = "";// Place the password for the MySQL database here 
$db_name = "test";// Place the name for the MySQL database here 
$conn = new mysqli($db_host, $db_username, $db_pass,$db_name); 

// Check connection 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 
} 
//echo "Connected successfully"; 
?> 

这完美的作品。