Php MySQL连接函数不支持';不行?

Php MySQL连接函数不支持';不行?,php,mysql,mysql-connect,Php,Mysql,Mysql Connect,我正在尝试连接到我的数据库,但它显示了mysql_connect函数中的错误 错误是: 致命错误:未捕获错误:在C:\xampp\htdocs\connect.php:12堆栈跟踪:#0 C:\xampp\htdocs\Test.php(3):C:\xampp\htdocs\connect.php的第12行抛出require()#1{main}函数 连接文件: <?php $db_host = "localhost"; // Place the username for the M

我正在尝试连接到我的数据库,但它显示了mysql_connect函数中的错误

错误是: 致命错误:未捕获错误:在C:\xampp\htdocs\connect.php:12堆栈跟踪:#0 C:\xampp\htdocs\Test.php(3):C:\xampp\htdocs\connect.php的第12行抛出require()#1{main}函数

连接文件:

<?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\u 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\u 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\u connect
是一个不推荐使用的函数。相反,您应该使用
mysqli\u connect
了解更多信息

以下代码应该可以工作:

<?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\u connect
是一个不推荐使用的函数。相反,您应该使用
mysqli\u connect
了解更多信息

以下代码应该可以工作:

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

php 5.5.0中不推荐使用mysql函数。我更改了您的代码中的一些更改。$符号已添加到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);

php 5.5.0中不推荐使用mysql函数。我更改了您的代码中的一些更改。$符号已添加到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";
?>


这非常有效。

尝试使用
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";
?>


这非常有效。

它是什么版本的PHP?您知道
mysql
函数已被弃用并从PHP7+中删除?您的PHP版本是什么。mysql\u connect此扩展在PHP5.5.0中被弃用,在PHP7.0.0中被删除此行错误
$con=mysql\u connect(“db\u主机”、“$db\u用户名”、“$db\u密码”)
那么这个代码在@Pixy_vxxc下永远不起作用你必须使用PDO或mysqli query如果你可以升级到PHP7,那么也可以升级到mysqli或PDO它是什么版本的PHP?您知道
mysql
函数已被弃用并从PHP7+中删除?您的PHP版本是什么。mysql\u connect此扩展在PHP5.5.0中被弃用,在PHP7.0.0中被删除此行错误
$con=mysql\u connect(“db\u主机”、“$db\u用户名”、“$db\u密码”)
那么这个代码在@Pixy_vxxc下永远不起作用。你必须使用PDO或mysqli查询如果你可以升级到PHP7,那么也可以升级到mysqli或PDO