【发布时间】:2016-07-09 17:55:26
【问题描述】:
我有一个 PHP 脚本,它查询数据库并返回我数据库中所有用户名的列表。问题是,它没有打印任何东西,我认为我的 while 语句不正确,这是我下面的代码:
<?php
include "init.php";
$stmt = "SELECT username FROM tbl_users ";
$result = $dbcon -> prepare($stmt);
$result->execute();
$result->store_result();
$count = 0;
while($row = mysqli_fetch_assoc($result)){
if($row['username']=="Ben"{
echo "Here";
}
//$count++;
}
?>
即使是这样基本的东西也行不通:
<?php
include "init.php";
$stmt = "SELECT username FROM tbl_users ";
$result = $dbcon -> prepare($stmt);
$result->execute();
$result->store_result();
$count = 0;
while($row = mysqli_fetch_assoc($result)){
echo "Here";
//$count++;
}
?>
我似乎也无法增加计数,甚至没有打印基本的“这里”
【问题讨论】:
-
您确定连接到数据库的用户有权查看该表吗?你确定表中有记录吗?您可以尝试“SELECT count(*) FROM tbl_users”,因为它应该返回一行(假设没有错误)。
-
您在表格中有用户名
Ben吗?你注释掉了count++,没有$,真的是count++而不是$count++吗? -
嗨,本数据库中确实存在
-
为什么要使用 oop 和程序风格?
$result->fetchAll()的输出是什么? -
@dex
->prepare()/->execute()也是 MySQLi 方法,->store_result()只是 MySQLi。除了while($row = mysqli_fetch_assoc($result))中的mysqli_fetch_assoc()