基本Php/Mysql搜索:rowCount()返回错误
我正在开发一个非常基本的php/mysql搜索。搜索基于下拉菜单中选择的值的筛选条件。有三个选项基本Php/Mysql搜索:rowCount()返回错误,php,Php,我正在开发一个非常基本的php/mysql搜索。搜索基于下拉菜单中选择的值的筛选条件。有三个选项整个站点,页面,博客。在我的php代码中,我准备sql语句,然后根据选择的过滤器执行相应的操作。我在$count=$query->rowCount()上遇到了一个错误,但最有趣的是,当我尝试回显我得到的1查询结果时。如何显示正确的值并克服错误 PHP/Mysql 尝试$fetchColumn而不是$rowCountrowCount()函数只返回受DELETE、INSERT或UPDATE语句影响的行数。
整个站点
,页面
,博客
。在我的php代码中,我准备sql语句,然后根据选择的过滤器执行相应的操作。我在$count=$query->rowCount()
上遇到了一个错误,但最有趣的是,当我尝试回显我得到的1
查询结果时。如何显示正确的值并克服错误
PHP/Mysql
尝试$fetchColumn
而不是$rowCount
rowCount()函数只返回受DELETE、INSERT或UPDATE语句影响的行数。要获取SELECT语句的行数,必须执行以下单独的查询:
<?php
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* Issue the real SELECT statement and work with the results */
$sql = "SELECT name FROM fruit WHERE calories > 100";
foreach ($conn->query($sql) as $row) {
print "Name: " . $row['NAME'] . "\n";
}
} else { /* No rows matched -- do something else */
print "No rows matched the query.";
}
}
$res = null;
$conn = null;
?>
这是PHP手册中的内容,如下所示:
rowCount()
是语句的$sqlCommand
方法,而不是其返回值$sqlCommand->rowCount()
您得到的是1
,因为PDOStatement::execute()
在成功/失败时返回的是true/false
。您的查询成功,因此变量$query
包含值'true.@MichaelBerkowski修复了不兼容问题。它最初是在mysql\uuu
中,但现在我将它与PDO
一起使用。OP没有使用名为$rowCount
的变量。也许您指的是使用@BoydDensmore提到的单独的“计数”查询?
<h2>Search the Exercise Tables</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For:
<input name="searchquery" type="text" size="44" maxlength="88">
Within:
<select name="filter1">
<option value="Whole Site">Whole Site</option>
<option value="Pages">Pages</option>
<option value="Blog">Blog</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
<?php
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
/* Issue the real SELECT statement and work with the results */
$sql = "SELECT name FROM fruit WHERE calories > 100";
foreach ($conn->query($sql) as $row) {
print "Name: " . $row['NAME'] . "\n";
}
} else { /* No rows matched -- do something else */
print "No rows matched the query.";
}
}
$res = null;
$conn = null;
?>