PHP报告说我正在以布尔形式提供...我不是

时间:2013-05-01 22:00:01

标签: php boolean

我正在为自己创建一个简单的博客系统 - 它已经完成,但是每当我查看帖子页面时,PHP都会发出错误,告诉我如何将一个布尔值提供给mysqli_fetch_array。最后我查了一下,我正在查询的查询吐出一个数字。

有什么想法吗?

代码:

$numPostsQry = $database->queryDB("SELECT COUNT(*) FROM mainSite_blogarticles WHERE articleCat='News'");
$numPosts = $database->fetchArray($numPostsQry);

fetchArray只是mysqli_fetch_array的类别名,queryDB是mysqli_query的别名。


我的两个函数fetchArray和queryDB的代码是:

查询 -

public function queryDB($query) {
    try {

        if ($this->connector == 'mysql') {
            return mysql_query($query);
        }

        elseif ($this->connector == 'mysqli') {
            return mysqli_query($this->connection,$query);
        }

    } catch (exception $e) {
        return $e;
    }
}

取 -

public function fetchArray($toFetch) {
    try {

        if ($this->connector == 'mysql') {
            return mysql_fetch_array($toFetch);
        }

        elseif ($this->connector == 'mysqli') {
            return mysqli_fetch_array($toFetch);
        }

    } catch (exception $e) {
        return $e;
    }
}

编辑(如果需要,全班):

<?php

class database {

    private $host;
    private $user;
    private $pass;
    private $db;
    private $connector;
    public  $connection;


    function __construct($host, $user, $pass, $db, $connector='mysql') {
        $this->host      = $host;
        $this->user      = $user;
        $this->pass      = $pass;
        $this->db        = $db;
        $this->connector = strtolower($connector);
    }


    public function openConnection() {
        try {

            if ($this->connector == 'mysql') {
                $this->connection = mysql_connect($this->host,$this->user,$this->pass);
                if (mysql_errno()) {
                    printf('Failed to connect: %s', mysql_error());
                } else {
                    mysql_select_db($this->db, $this->connection);
                }
            }

            elseif ($this->connector == 'mysqli') {
                $this->connection = mysqli_connect($this->host,$this->user,$this->pass);
                if (mysqli_errno($this->connection)) {
                    printf('Failed to connect: %s', mysqli_error());
                } else {
                    mysqli_select_db($this->connection, $this->db);
                }
            }

        } catch (exception $e) {
            return $e;
        }
    }


    public function closeConnection() {
        try {

            if ($this->connector == 'mysql') {
                mysql_close();
            }

            elseif ($this->connector == 'mysqli') {
                mysqli_close($this->connection);
            }

        } catch (exception $e) {
            return $e;
        }
    }

    public function slashString($string) {
        try {

            return addslashes($string);

        } catch (exception $e) {
            return $e;
        }
    }

    public function stripSlashString($string) {
        try {

            return stripslashes($string);

        } catch (exception $e) {
            return $e;
        }
    }

    public function escapeString($string) {
        try {

            if ($this->connector == 'mysql') {
                return mysql_real_escape_string($string);
            }

            elseif ($this->connector == 'mysqli') {
                return mysqli_real_escape_string($this->connection,$string);
            }

        } catch (exception $e) {
            return $e;
        }
    }


    public function queryDB($query) {
        try {

            if ($this->connector == 'mysql') {
                return mysql_query($query);
            }

            elseif ($this->connector == 'mysqli') {
                return mysqli_query($this->connection,$query);
            }

        } catch (exception $e) {
            return $e;
        }
    }

    public function dbNumRows($query) {
        try {

            if ($this->connector == 'mysql') {
                return mysql_num_rows($query);
            }

            elseif ($this->connector == 'mysqli') {
                return mysqli_num_rows($query);
            }

        } catch (exception $e) {
            return $e;
        }
    }

    public function fetchAssoc($toFetch) {
        try {

            if($this->connector == 'mysql') {
                return mysql_fetch_assoc($toFetch);
            }

            elseif($this->connector == 'mysqli') {
                return mysqli_fetch_assoc($toFetch);
            }
        } catch (exception $e) {
            return $e;
        }
    }

    public function fetchArray($toFetch) {
        try {

            if ($this->connector == 'mysql') {
                return mysql_fetch_array($toFetch);
            }

            elseif ($this->connector == 'mysqli') {
                return mysqli_fetch_array($toFetch);
            }

        } catch (exception $e) {
            return $e;
        }
    }
}

?>

1 个答案:

答案 0 :(得分:0)

尝试使用此代码:

$numPostsQry = $database->queryDB("SELECT COUNT(*) FROM mainSite_blogarticles WHERE articleCat='News'");

if ($numPostsQry ===false) {
  printf("Error: %s\n", mysqli_error($database));
  exit();
}

$numPosts = $database->fetchArray($numPostsQry);

你可以看到你收到错误的原因