将数据库转换为JSON

时间:2014-06-15 15:09:40

标签: php json database

我有一种方法可以将JSON创建为一个如下所示的数组:

[{"date":"","name":"","image":"","genre":"","info":"","videocode":""},{...},{...}]

我首先尝试从html页面(而不是数据库)获取数据,如下所示:

$arr = array();

$info = linkExtractor($html);
$dates = linkExtractor2($html);
$names = linkExtractor3($html);
$images = linkExtractor4($html);
$genres = linkExtractor5($html);
$videocode = linkExtractor6($html);

for ($i=0; $i<count($images); $i++) {
    $arr[] = array("date" => $dates[$i], "name" => $names[$i], "image" => $images[$i], "genre" => $genres[$i], "info" => $info[$i], "videocode" => $videocode[$i]);
}

echo json_encode($arr);

每个linkExtractor看起来有点像这样 - 它抓住了课程videocode中的所有文字。

function linkExtractor6($html){ 
    $doc = new DOMDocument(); 
    $last = libxml_use_internal_errors(TRUE); 
    $doc->loadHTML($html); 
    libxml_use_internal_errors($last); 
    $xp = new DOMXPath($doc); 
    $result = array(); 
    foreach ($xp->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' videocode ')]") as $node) 
        $result[] = trim($node->textContent); // Just push the result here, don't assign it to a key (as that's why you're overwriting)

    // Now return the array, rather than extracting keys from it
    return $result; 
}

我现在想用数据库来做这件事。

所以我试图用这个替换每个linkExtractor - 显然是连接:

function linkExtractor6($html){ 
    $genre = mysqli_query($con,"SELECT genre
    FROM entries
    ORDER BY date DESC");

    foreach ($genre as $node) 
            $result[] = $node; 
    return $result; 
} 

但我收到错误:

  

为foreach()提供的参数无效

3 个答案:

答案 0 :(得分:2)

避免冗余并运行一个SELECT

function create_json_db($con){ 
    $result = mysqli_query($con,"SELECT date, name, image, genre, info, videocode
                                 FROM entries
                                 ORDER BY date DESC");

    $items= array();
    while ($row = mysqli_fetch_assoc($result)) {
       $items[] = $row;
    }

    return $items ; 
} 

答案 1 :(得分:0)

尝试使用此功能。 the official PHP documentation中的更多信息:

function linkExtractor6($html){ 
    $result = mysqli_query($con,"SELECT genre
    FROM entries
    ORDER BY date DESC");

    $items = array();
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
       $items[] = $row;
    }

    return $items; 
} 

答案 2 :(得分:0)

首先,您不是通过类似mysqli_fetch_array的内容迭代您的结果。所以这里是mysqli_fetch_array的功能。但是有一个更大的问题。请继续阅读。

function linkExtractor6($html){ 
    $result = mysqli_query($con,"SELECT genre
    FROM entries
    ORDER BY date DESC");

    $ret = array();
    while ($row = mysqli_fetch_array($result)) {
       $items[] = $row;
    }

    return $ret ; 
} 

好的,完成后,它仍然无法正常工作。为什么?看看你的功能。特别是这一行:

$result = mysqli_query($con,"SELECT genre

$con来自哪里?没有数据库连接mysqli_query根本不起作用。因此,如果你以某种方式将$con置于函数之外,则需要将其传递给函数,如下所示:

function linkExtractor6($con, $html){ 

所以你的全部功能将是:

function linkExtractor6($con, $html){ 
    $result = mysqli_query($con,"SELECT genre
    FROM entries
    ORDER BY date DESC");

    $ret = array();
    while ($row = mysqli_fetch_array($result)) {
       $items[] = $row;
    }

    return $ret ; 
} 

请记住,功能是独立的&amp;除非你明确地将数据传递给它们,否则它们会发生在它们之外的任何事情中。