在 wampserver 中显示弃用错误

Shows Deprecated error in wampserver

我一直在尝试显示 MySQL 数据库的一些结果,该数据库有 5000 多行。

我已经尝试了 10 行,代码有效,但它显示警告,如屏幕截图所示!

在 wampserver 中显示错误

在线服务器也显示几乎相同的错误 请帮助我修复它,我对 SQL.

一无所知

    <?php
    class DBController
    {
        private $host = "localhost";
        private $user = "user";
        private $password = "1234";
        private $database = "new1";



        function __construct()
        {
            $conn = $this->connectDB();
            if (!empty($conn)) {
                $this->selectDB($conn);
            }
        }

        function connectDB()
        {
            $conn = mysql_connect($this->host, $this->user, $this->password);
            return $conn;
        }

        function selectDB($conn)
        {
            mysql_select_db($this->database, $conn);
        }

        function runQuery($query)
        {
            $result = mysql_query($query);
            while ($row = mysql_fetch_assoc($result)) {
                $resultset[] = $row;
            }
            if (!empty($resultset))
                return $resultset;
        }

        function numRows($query)
        {
            $result   = mysql_query($query);
            $rowcount = mysql_num_rows($result);
            return $rowcount;
        }
    }
    ?>

请查收

<?php
    function perpage($count, $per_page = '10',$href) {
        $output = '';
        $paging_id = "link_perpage_box";
        if(!isset($_POST["page"])) $_POST["page"] = 1;
        if($per_page != 0)
        $pages  = ceil($count/$per_page);
        if($pages>1) {

            if(($_POST["page"]-3)>0) {
                if($_POST["page"] == 1)
                    $output = $output . '<span id=1 class="current-page">1</span>';
                else                
                    $output = $output . '<input type="submit" name="page" class="perpage-link" value="1" />';
            }
            if(($_POST["page"]-3)>1) {
                    $output = $output . '...';
            }

            for($i=($_POST["page"]-2); $i<=($_POST["page"]+2); $i++)    {
                if($i<1) continue;
                if($i>$pages) break;
                if($_POST["page"] == $i)
                    $output = $output . '<span id='.$i.' class="current-page" >'.$i.'</span>';
                else                
                    $output = $output . '<input type="submit" name="page" class="perpage-link" value="' . $i . '" />';
            }

            if(($pages-($_POST["page"]+2))>1) {
                $output = $output . '...';
            }
            if(($pages-($_POST["page"]+2))>0) {
                if($_POST["page"] == $pages)
                    $output = $output . '<span id=' . ($pages) .' class="current-page">' . ($pages) .'</span>';
                else                
                    $output = $output . '<input type="submit" name="page" class="perpage-link" value="' . $pages . '" />';
            }

        }
        return $output;
    }

    function showperpage($sql, $per_page = 10, $href) {     
        $result  = mysqli_query($sql);
        $count   = mysqli_num_rows($result);
        $perpage = perpage($count, $per_page,$href);
        return $perpage;
    }

?>

好吧,我自己承担风险,回答迁移后的代码。通过检查 phpinfo(); 确保安装了 MySQLi。您的新代码:

<?php
    class DBController
    {
        private $host = "localhost";
        private $user = "user";
        private $password = "1234";
        private $database = "new1";
        private $conn;

        function __construct()
        {
            $this->conn = $this->connectDB();
            if (!empty($conn)) {
                $this->selectDB($conn);
            }
        }

        function connectDB()
        {
            $conn = mysqli_connect($this->host, $this->user, $this->password, $this->database);
            return $conn;
        }

        function runQuery($query)
        {
            $result = mysqli_query($this->conn, $query);
            while ($row = mysqli_fetch_assoc($result)) {
                $resultset[] = $row;
            }
            if (!empty($resultset))
                return $resultset;
            return false;
        }

        function numRows($query)
        {
            $result   = mysqli_query($this->conn, $query);
            $rowcount = mysqli_num_rows($this->conn, $result);
            return $rowcount;
        }
    }
?>

备注:

  1. 新建私有变量$conn.
  2. 现在没有 select_db() 功能。
  3. runQuery 添加了更好的 return

原因:Why shouldn't I use mysql_* functions in PHP?

希望这对您有所帮助。