MySQL 连接功能不起作用?
MySQL Connect Function doesn't work?
我正在尝试连接到我的数据库,但它显示 mysql_connect 函数出错。
错误是:
致命错误:未捕获错误:在 C:\xampp\htdocs\Connect.php:12 中调用未定义函数 mysql_connect() 堆栈跟踪:#0 C:\xampp\htdocs\Test.php(3): require() #1 {main} throw in C:\xampp\htdocs\Connect.php on line 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";
?>
为什么要同时使用 mysql_connect 甚至 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();
}
函数 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();
}
?>
mysql 函数在 php 5.5.0 中已弃用。我已经更改了您的代码中的一些更改。在 mysqli connect 语句中添加了 $ 符号。
$con = mysqli_connect("$db_host","$db_username","$db_pass");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db("$db_name", $con);
尝试使用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";
?>
这非常有效。
我正在尝试连接到我的数据库,但它显示 mysql_connect 函数出错。
错误是: 致命错误:未捕获错误:在 C:\xampp\htdocs\Connect.php:12 中调用未定义函数 mysql_connect() 堆栈跟踪:#0 C:\xampp\htdocs\Test.php(3): require() #1 {main} throw in C:\xampp\htdocs\Connect.php on line 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";
?>
为什么要同时使用 mysql_connect 甚至 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();
}
函数 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();
}
?>
mysql 函数在 php 5.5.0 中已弃用。我已经更改了您的代码中的一些更改。在 mysqli connect 语句中添加了 $ 符号。
$con = mysqli_connect("$db_host","$db_username","$db_pass");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db("$db_name", $con);
尝试使用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";
?>
这非常有效。