从数组加载特定数据

时间:2016-02-14 17:33:26

标签: php mysql arrays

所以我有这个类用于从我的MySQL数据库加载数据:

class Db {

    protected static $connection;

    public function connect() {    
        if(!isset(static::$connection)) {
            $config = parse_ini_file('config.ini'); 
            static::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
        }
        if(static::$connection === false) {
            return false;
        }
        return static::$connection;
    }


    public function query($query) {
        return $this->connect()->query($query);
    }

    public function select($query) {
        $rows = array();
        $result = $this->query($query);
        if($result === false) {
            return false;
        }
        while ($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }

    public function error() {
        return $this->connect()->error;
    }

    public function quote($value) {
        return "'" . $this->connect()->real_escape_string($value) . "'";
    }
}

我像这样使用那个类:

$db = new Db();
$rows = $db -> select("SELECT name, shortlink FROM `test` WHERE id=3");

它给了我一个包含数据的数组。

问题在于我想提取特定数据,例如短链接字段。

我该怎么做?我尝试了echo $rows['shortlink'],但这给了我以下错误:

  

未定义的索引:短链接

那么如何打印特定数据?

1 个答案:

答案 0 :(得分:1)

您返回的#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE_SIZE 100 #define INITIAL_BUFFER_SIZE 16 int main() { char **line_buffer; line_buffer = (char **) malloc(INITIAL_BUFFER_SIZE); int i = 0; int lines = 0; // the size of buffer int buffer_size = 1; int *buffer_size_p = &buffer_size; char *line_one = (char *) malloc(MAX_LINE_SIZE); //read lines from a file while (gets(line_one)) { line_buffer[lines] = (char *) malloc(MAX_LINE_SIZE); strcpy(line_buffer[lines], line_one); lines++; line_one = (char *) malloc(MAX_LINE_SIZE); // if too much lines double the buffer size if (lines == *buffer_size_p) { buffer_size *= 2; line_buffer = IncreaseBuffer(line_buffer, buffer_size_p); } } PrintLines(line_buffer, lines); // sorting all the line by strcmp for (i = 0; i < lines; i++) { printf("%s", line_buffer[i]); // int min = MinLineIndex(line_buffer, i, lines); // SwapLines(line_buffer, i, min); } PrintLines(line_buffer, lines); // free(line_buffer); return 0; } 列是一组关联数组,用于提取从查询返回的$rows数据,您必须执行以下操作:

shortlink