将所有mysql函数切换到mysqli函数会导致警告错误

mbskvtky  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(364)

我目前正在将所有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";
    }
}

尝试了很多解决方案,但都不适合我
如上所示获取错误。请帮我解决这个。。
提前谢谢。

epggiuax

epggiuax1#

我认为你的数据库配置有问题。
将config.php文件的以下代码替换为bellow。

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

luaexgnf2#

您可以尝试将对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 );

?>

相关问题