【问题标题】:After a website migration i am getting an MySQL error Deprecated: mysql_pconnect(): [duplicate]网站迁移后,我收到 MySQL 错误已弃用:mysql_pconnect():[重复]
【发布时间】:2017-05-25 04:50:59
【问题描述】:

对不起,伙计们,我对 PHP 了解不多,我需要一些帮助,我将网站从一台服务器迁移到另一台服务器,看来旧服务器的操作系统和 PHP 版本较旧 将文件移至新服务器和数据库后,我运行该站点并获取

不推荐使用:mysql_pconnect():不推荐使用 mysql 扩展,并且 将来将被删除:使用 mysqli 或 PDO 代替 /var/www/classes/dbcon.class.php 第 45 行

这是该文件中的代码,我已尝试更改几行并尝试使用 MySqli,但我似乎无法使其正常工作。

<?php



class dbCon
{
    //
    // private variables
    // 

    var $_hostname_Con;
    var $_database_Con; 
    var $_username_Con;
    var $_password_Con;
    var $_Con; 

    var $_result;   
    var $_hasData;
    var $_lastQuery;
    var $_row;
    var $_rowCount;

    //
    // methods (private)
    // 

    //
    // methods (public)
    // 

    // constructor
    function dbCon()

    {
        $this->_hostname_Con = "localhost";
        $this->_database_Con = "street";
        $this->_username_Con = "rt";
        $this->_password_Con = "mwL";
        //*
        $this->_database_Con = "cranes_cms";
        $this->_username_Con = "t";
        $this->_password_Con = "mob";
        //*/

        $this->_Con = mysql_pconnect ($this->_hostname_Con, $this->_username_Con, $this->_password_Con) or trigger_error(mysql_error(),E_USER_ERROR); 
        mysql_select_db($this->_database_Con) or die('Could not select database');
    }

    function freeResult()
    {
        mysql_free_result($this->_result);      
    }

    function close()
    {
        mysql_close($this->_Con);   
    }


    function update($inQuery)
    {
        //reset row counter and data flag
        $this->_rowCount = 0;
        $this->_hasData = FALSE;

        //do SQL
        $this->_lastQuery = $inQuery;
        mysql_query($this->_lastQuery,$this->_Con) or die('Query failed: ' . mysql_error());
    }

    function select($inQuery)
    {
        //reset row counter and data flag
        $this->_rowCount = 0;
        $this->_hasData = FALSE;

        //do SQL
        $this->_lastQuery = $inQuery;
        $this->_result = mysql_query($this->_lastQuery,$this->_Con) or die('Query failed: ' . mysql_error());

        //set has data flag
        if (mysql_num_rows($this->_result))
            {$this->_rowCount = mysql_num_rows($this->_result);}
        else
            {$this->_rowCount = 0; }

        if ( $this->_rowCount > 0) 
            {$this->_hasData = TRUE;} 
        else 
            {$this->_hasData = FALSE;}

    }

    function getData()
    {
    if ($this->_hasData)
        {
            if ($this->_row = mysql_fetch_array($this->_result, MYSQL_ASSOC))
                {
                    return $this->_row;
                }
            else
                {
                    return FALSE;
                }
        }
    else
        {
        return FALSE;
        }
    }

    function getRowCount()
    {
        return $this->_rowCount;
    }   

    function hasData()
    {
        return $this->_hasData;
    }
// end 
}

?>

这是别人的代码,我不知道在哪里解决这个问题。有人可以请吗

【问题讨论】:

  • mysql_*() 已弃用。所以使用 mysqli_*()
  • 正如我所说,我对 PHP 了解不多,我尝试用 mysqli 替换 mysql_pconnect 行,但看起来并不那么简单

标签: php mysql mysqli pdo


【解决方案1】:

这是一个使用 mysqli_*() 的示例,您可以了解更多信息here

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);

    /* free result set */
    $result->close();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2011-06-03
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    相关资源
    最近更新 更多