phpmysqlidescribe

mysqli DESCRIBE returns only one row


Structure:

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

I'm experiencing a weird problem here, query DESCRIBE links returns only first row (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}).");
    }
}

So those are my functions taken out of a class (ask if more needed...), and yes, the performed query returns only first row, but $this->affected returns the correct value of 5 in this case.

Returns anticipated result when query is run within phpMyAdmin.

What could be the problem?


Solution

  • Ah, found out the problem myself, looks like my fetch() function returned only first row. Changed it a little and it works:

    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;
    }
    

    Damn those heedless mistakes...