【问题标题】:MySQL Connect Function doesn't work?MySQL 连接功能不起作用?
【发布时间】: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


【解决方案1】:

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

这很好用。

【讨论】:

    【解决方案2】:

    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);
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        你为什么同时使用 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();
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-30
          • 1970-01-01
          • 2018-08-25
          • 2017-08-12
          • 1970-01-01
          • 2017-12-13
          • 1970-01-01
          • 2017-01-24
          • 2015-03-16
          相关资源
          最近更新 更多