2

结构:

CREATE TABLE `links` (
 `id` int(11) NOT NULL auto_increment,
 `title` int(11) default NULL,
 `url` varchar(200) NOT NULL,
 `seourl` varchar(150) default NULL,
 `linkset` varchar(50) NOT NULL default 'main',
 PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

我在这里遇到了一个奇怪的问题,查询DESCRIBE links只返回第一行(id)。

// my query...
$data = $db->query('DESCRIBE `links`')->fetch();

// query function
public function query($sql, $id = false){
    $this->query_id = mysqli_query($this->connection, $sql);
    if(!$this->query_id){
        psyo::error("Error while executing query (sql: {$sql}).");
        return NULL;
    }else{
        $this->result = mysqli_store_result($this->connection);
        $this->affected = mysqli_affected_rows($this->connection);
        return $id ? $this->query_id : $this;
    }
}

// fetch function
public function fetch($query_id = NULL){
    if($query_id)
        $this->query_id = $query_id;
    if($this->query_id){
        $data = mysqli_fetch_assoc($this->query_id);
    }else{
        psyo::error("Error while fetching results (query id: {$this->query_id}).");
    }
    $this->free();
    return $data;
}

// connection set by
public function connect(){
    $this->connection = mysqli_connect($this->server, $this->username, $this->password, $this->database);
    if(!$this->connection){
        psyo::error("Error connecting to server or while selecting database (database: {$this->database}).");
    }
}

所以这些是我从一个类中取出的函数(询问是否需要更多......),是的,执行的查询只返回第一行,但在这种情况下$this->affected返回正确的值。5

在 phpMyAdmin 中运行查询时返回预期结果。

可能是什么问题呢?

4

1 回答 1

1

啊,我自己发现了问题,看起来我的fetch()函数只返回了第一行。稍微改变了一下,它的工作原理:

public function fetch($all = false, $query_id = NULL){
    if($query_id)
        $this->query_id = $query_id;
    if($this->query_id){
        if($all){
            while($row = mysqli_fetch_assoc($this->query_id)){
                $data[] = $row;
            }
        }else{
            $data = mysqli_fetch_assoc($this->query_id);
        }
    }else{
        psyo::error("Error while fetching results (query id: {$this->query_id}).");
    }
    $this->free();
    return $data;
}

该死的那些粗心的错误......

于 2011-04-04T12:22:05.533 回答