-1

我有个问题。我想将函数的值作为 mysql 结果返回

function Gets_comments_array_from_db($type, $thread_comment_id) {
# Sets variable for Query
    $The_TYPE = $type;
    $Thread = mysql_real_escape_string(intval($thread_comment_id));

# return array query for Further working
    return mysql_query("SELECT * FROM `site_comments` WHERE `type`='".$The_TYPE."' AND `thread_comment_id`='".$Thread."'");

}

while($comment = mysql_fetch_array(Gets_comments_array_from_db('news', $id)){
                                                    echo '11';
                                                }

谢谢!:)

4

1 回答 1

0

正如dbf所说,您在 while 循环的每次迭代中都执行查询。您需要像这样更改代码:

$rs = Gets_comments_array_from_db('news', $id);

while($comment = mysql_fetch_assoc($rs)) {
     echo '11';
}

现在您将获得一次结果,然后在 while 循环中遍历它们。同样在循环内,您将能够使用列名作为数组键来处理值。

while($comment = mysql_fetch_assoc($rs)) {
     $field1 = $comment['field1'];
     $field2 = $comment['field2'];
     $field3 = $comment['field3'];
}

如果你像这样构造你的 mysql 查询

SELECT Field1, Field2, Field3, Field4 FROM {table} WHERE {where_clause}

您将能够像这样在 while 循环中设置变量

$rs = Gets_comments_array_from_db('news', $id);

while(list($field1, $filed2, $filed3, $field4) = myslq_fetch_row($rs)) {
    // use variables here
}
于 2012-09-22T18:58:25.010 回答