MySQL到MySQLi的连接问题 [英] MySQL to MySQLi connection problems

查看:64
本文介绍了MySQL到MySQLi的连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级了服务器,现在我正尝试将所有内容从MySQL转换为MySQLi,但遇到了一些困难.

I recently upgraded my server and now I'm trying to convert everything from MySQL to MySQLi and I'm having some difficulty.

我不断收到此错误:

PHP Warning:  mysqli_select_db() expects exactly 2 parameters

有人可以引导我朝正确的方向前进,谢谢!!!

Can someone guide me in the right direction please and thanks!!!

这是我在PHP类中的旧代码:

Here is my old code that is in a PHP class:

class Db {
    function __construct($app = ''){
        // Get the main settings from the array we just loaded
        $host = 'localhost';
        $user = 'username';
        $pass = 'password';
        $db = 'db_name';
        // Connect to the database
        $this->link = mysqli_connect($host, $user, $pass);
        mysqli_select_db($db);
        register_shutdown_function(array(&$this, 'close'));
    }
    function query($query){
        if($query) return @mysqli_query($query, $this->link);
        else return false;
    }
    function fetchArray($result){
        if($result) return mysqli_fetch_array($result, mysqli_ASSOC);
        else return false;
    }
    function queryFetch($query){
        if($query) $result = mysqli_query($query, $this->link);
        if($result)
            return mysqli_fetch_array($result, mysqli_ASSOC);
        else
            return false;
    }
    function close(){
        mysqli_close($this->link);
    }
}

推荐答案

@mysqli_query($query, $this->link)连接在这里排在第一位,而不是第二位;与mysql_ API相比,使用mysqli_ API的顺序有所不同.另外,在测试/开发过程中删除@,它是一个错误抑制器.

@mysqli_query($query, $this->link) the connection comes first here and not second; the order is different in using the mysqli_ API as opposed to the mysql_ API. Plus, remove @ during testing/development, it's an error suppressor.

mysqli_select_db($db);需要数据库连接,并作为第一个参数.

mysqli_select_db($db); requires a db connection and as the first argument.

全部在手册中:

  • http://php.net/manual/en/mysqli.query.php
  • http://php.net/manual/en/mysqli.select-db.php

注意:您可能存在变量范围问题,因此,在这种情况下,您将需要在方法内部传递连接.

Note: You may potentially have a variable scope issue, so if that is the case, you will need to pass the connection inside the methods.

使用错误处理:

  • http://php.net/manual/en/mysqli.error.php
  • http://php.net/manual/en/function.error-reporting.php

这篇关于MySQL到MySQLi的连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆