簡體   English   中英

將所有mysql_ *函數切換為mysqli_ *函數會導致警告錯誤

[英]Switching all the mysql_* functions to mysqli_* functions results in warning errors

我目前將所有mysql_ *函數切換為mysqli_ *函數,並且遇到以下錯誤。

 PHP Warning: mysqli_connect(): (HY000/1040): Too many connections in

 PHP Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in 

 PHP Warning: mysqli_error() expects parameter 1 to be mysqli, boolean 

在config.php中:

            $dbuser = "xxx";
            $dbpass = "xxx";
            $dbhost = "xxxx";
            $dbname = "xxxxxx";
            $connection = mysqli_connect($dbhost, $dbuser, $dbpass) or die("could not connect to mysql");
            if($connection){
                echo "\n Database connected ....\n\n";
            }
            mysqli_select_db($connection,$dbname) or die(mysqli_error($connection));

在other.php中

require_once 'Lib/config.php';

 class Mutex {

    protected $connection;
    public function __construct()
    {
    global $dbuser,$dbpass,$dbname,$dbhost; // globally declaring the config variables
    $this->connection = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);   
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      //you need to exit the script, if there is an error
        exit();
    }
    }



    public function Prog($pack = null) {

        if(isset($pack) && $pack != "") {
            $sql_qry    = "SELECT id from xxxx WHERE package like '" . addslashes($pack) . "'";
            showSqlQuery($sql_qry);
            $result     = mysqli_query($this->connection,$sql_qry)or die(mysqli_error($this->connection));
            if($result && mysqli_num_rows($result)>0) {
                $row    = mysqli_fetch_assoc($result);
                if(!empty($row)) {
                    return "Exists";
                }
            }
            return "NotExists";
        }
        return "InvalidProcess";
    }
}

嘗試了許多解決方案,但沒有一個對我有用

如上所示出現錯誤。 請幫我解決這個問題。

提前致謝。

您可以嘗試以下方法,而不是復制(幾乎)連接:將對db連接的引用作為參數傳遞給Mutex構造函數

<?php
    /* lib/config.php */
    $dbhost =   'localhost';
    $dbuser =   'root'; 
    $dbpass =   'xxx'; 
    $dbname =   'xxx';
    $db =   new mysqli( $dbhost, $dbuser, $dbpass, $dbname );

?>

<?php

    include 'lib/config.php';

    class Mutex{
        private $connection;

        public function __construct( $dbo=object ){
            $this->connection=$dbo;
        }

        public function Prog($pack = null) {
            $result='InvalidProcess';
            if( !empty( $pack ) ) {

                $sql='select `id` from `xxxx` where `package` like ?';
                $stmt=$this->connection->prepare( $sql );
                if( $stmt ){

                    $var = '%'.$pack.'%';
                    $stmt->bind_param('s', $var );

                    $result=$stmt->execute();
                    if( $result && $stmt->num_rows > 0  ){

                        $stmt->store_result();
                        $stmt->bind_result( $id );

                        while( $stmt->fetch() ){/* do stuff perhaps */
                            echo $id;
                        }

                        $stmt->free_result();
                        $stmt->close();
                        $result='Exists';
                    }
                }
            }
            return $result;
        }



    $pack='banana';

    $mutex=new Mutex( $db );
    $mutex->Prog( $pack );

?>

我認為您的數據庫配置有一些問題。

將下面的config.php文件代碼替換為以下內容。

$dbuser = "xxx";
$dbpass = "xxx";
$dbhost = "xxxx";
$dbname = "xxxxxx";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM