面向对象的数据库连接

时间:2015-03-24 21:18:13

标签: php mysql mysqli

我正在尝试连接到我所在的本地主机。我无法解决我一直遇到的这些错误。任何人都可以帮忙吗?这是代码。

class mymysqli {
    public $db;
    function connectdb ($hostname, $database, $mysqli_login, $mysqli_password){
        $db=mysqli_connect ($hostname, $mysqli_login, $mysqli_password) or die     ('There is an issue');
        mysqli_select_db($database, 'conectdb');
        return $db;
    }
    function selectRows ($query){
        $resultSet=mysqli_query($query);
        if(mysqli_num_rows($resultSet) > 0){
            return $resultSet;
        }
        else{
            return false;
        }
    }
}

$DBConnect = new mymysqli();
$db=$DBConnect->connectdb("localhost","nwtip","root","");
if($db){
    $SQL = "SELECT color FROM colors ORDER BY color";
    $rs = $DBConnect -> selectRows($SQL);
    if ($rs){
        $intcount=0;
        while(mysqli_fetch_row($rs)){
            echo mysqli_result($rs,$intcount,"color")."<br>";
            $intcount++;
        }
    }
}

这些是错误 警告:mysqli_select_db()期望参数1为mysqli,第10行的C:\ wamp \ www \ inclass.php中给出的字符串

警告:mysqli_query()需要至少2个参数,1在第14行的C:\ wamp \ www \ inclass.php中给出

警告:mysqli_num_rows()要求参数1为mysqli_result,在第15行的C:\ wamp \ www \ inclass.php中给出null

1 个答案:

答案 0 :(得分:1)

$db=mysqli_connect ($hostname, $mysqli_login, $mysqli_password) or die 
由于范围

$ db首先没有被设置

mysqli_select_db($database, 'conectdb'); 

应该阅读

mysqli_select_db($db, 'conectdb');