循环遍历mySQL并打印为json

时间:2015-04-20 15:56:30

标签: php mysql json

我正在尝试使用两个GET请求遍历MYSQL数据库。但它似乎没有用?这是我回来的信息:

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in /path on line 13
[]

这是我的代码:

$con = mysqli_connect('localhost','user','pass','db');

$limit = $_GET['limit'];
$lastid = $_GET['lastid'];



$all_news = $con->prepare("SELECT * FROM news ORDER BY news.date DESC LIMIT ?, ?"); 
$all_news->bind_param("ii", $lastid, $limit);
$all_news->execute();
$all_news->bind_result($id, $url, $image_url, $date, $news_text, $referer_img);

$rows = array();
while($all_news->fetch()) {
    $rows[] = array($id, $url, $image_url, $date, $news_text, $referer_img);
}
print json_encode($rows);

2 个答案:

答案 0 :(得分:1)

您有select *,但只将一个变量绑定到结果列。如果您只想要一列,则只显式选择一列:

SELECT field_you_want FROM news ...

答案 1 :(得分:0)

该错误是因为您只绑定了一个参数,并且您的语句包含两个变量。