这是我的代码,但不显示我的sql输出,而是显示0
<html>
<head>
</head>
<body>
<?php
$sql1 = "SELECT COUNT(c.enno) AS 'noreg' FROM dtr_empinfo c WHERE c.`stats` =
'Regular'";
$query1 = $dbh -> prepare($sql);
$query1->execute();
$results=$query1->fetchAll(PDO::FETCH_OBJ);
$empcount=$query1->rowCount();
?>
<?php echo htmlentities($empcount);?>
</body>
</html>
答案 0 :(得分:0)
您在此处使用“ $ sql 1 ”:
$sql1 = "SELECT COUNT(c.enno) AS 'noreg' FROM dtr_empinfo c WHERE c.`stats` =
'Regular'";
,但是您在此处使用“ $ sql”:
$query1 = $dbh -> prepare($sql);
因此,您正在运行一个空的sql命令,因此返回0行。
此外,您的echo语句仅返回行数。有关回显实际结果的说明,请参见下面的代码。
我曾经测试过的完整代码:
<html>
<head>
</head>
<body>
<?php
// Create PDO
$dbh = new PDO("mysql:host=localhost;dbname=your_database_name", 'your_database_username', 'your_database_user_password');
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT COUNT(c.enno) AS 'noreg' FROM dtr_empinfo c WHERE c.`stats` = 'Regular'";
$query1 = $dbh -> prepare($sql);
$query1->execute();
$results=$query1->fetchAll(PDO::FETCH_OBJ);
$empcount=$query1->rowCount();
// You're only requesting the row count with this statement:
$empcount=$query1->rowCount();
echo htmlentities($empcount);
// If you want the actual SQL results, do this:
echo $results[0]->noreg;
?>
<!-- Or, you can echo it within the html, like this: -->
<?php echo $results[0]->noreg; ?>
<!-- No htmlentities needed, since it's just a number -->
</body>
</html>