致命错误:在工作查询的非对象上调用成员函数rowcount()

时间:2013-04-13 11:28:31

标签: php mysql

我正在运行此查询

try
{
$park_id = $_GET['park_id'];
$query2="SELECT `name` FROM  `tpf_rides` WHERE  `park_id` = $park_id AND  

`top_ride` = 1 ORDER BY `name` ASC";
$result2 = $pdo->query($query2);
}
catch (PDOException $e)
{
$output = 'Unable to pull rides.';
include 'output.html.php';
}

$output = 'Sucessfully pulled rides';
//include 'output.html.php';//

并用

显示
<?php foreach ($result2 as $row2): ?>

<li><h3><?php echo $row2['name']; ?></h3></li>

<?php endforeach; ?>

一切正常,“顶级游乐设施”列表正确显示。

问题是我正在尝试添加一个if语句,以便如果某个特定的“公园”没有“顶级游乐设施”,那么该部分页面就不会显示。下面是我正在努力工作的代码,但是我在第133行的/home/danville/public_html/parkindex/park.php中的非对象上收到错误“致命错误:调用成员函数rowcount()”这条bieng line 133“if($ count2 = $ query2-&gt; rowcount()&lt; 1)”有人可以帮忙吗?

if ($count2 = $query2->rowcount() < 1)

{
    echo "No results found";
} 

else 

{
 echo "<h2>Top Attractions</h2>";

foreach ($result2 as $row2){

echo "<li><h3>{$row2['name']}</h3></li>";

}


}

4 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。事实证明,修复是准备,然后执行。

$result = $pdo->prepare($query2);
$result->execute();
$count = $result->rowCount();

奇怪的是,rowCount是从$ pdo-&gt; query();返回的对象的函数,它看起来像是一个内部故障。

尝试:

var_dump(get_class_methods($result)); 

看看我的意思。

答案 1 :(得分:0)

获取结果的行数,而不是查询字符串

的行数
if ($count2 = $result2->rowcount() < 1)

if ($count2 = $query2->rowcount() < 1)

答案 2 :(得分:0)

在我看来,有一些不符合逻辑的东西。如果您已经有$park_id并且您匹配一个特定行,那么只有在top_rides = 1时才能将结果处理为appair 所以我会改变查询如下

$query2="SELECT `name`, `top_rides` FROM  `tpf_rides` WHERE `park_id` = '".$park_id."'";

现在我们已准备好放置if条件并仅显示top_rides的结果 我认为当公园有最佳游乐设施时,此字段设置为1

foreach ($result2 as $row2)
{
    if($row2['top_rides'] == '1')
    {
         echo '<li><h3>'. $row2['name'] .'</h3></li>';
    } 
    else
    {
         echo "No results found";
    }
}

您必须更改yuor查询以获取park_top字段以及

$query2="SELECT `name` FROM  `tpf_rides` WHERE  `park_id` = $park_id AND  `top_ride` = 1 ORDER BY `name` ASC";

答案 3 :(得分:0)

这是我为页面运行的所有查询。这里讨论的查询是query2

<?php

try
{
$pdo = new PDO('mysql:host=localhost;dbname=danville_tpf', 'user',
'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}


$output = 'Theme Park Database initialized';
//include 'output.html.php';//

try
{
$park_id = $_GET['park_id'];
$query="SELECT * FROM tpf_parks WHERE park_id = $park_id";
$result = $pdo->query($query);
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
//include 'output.html.php';//
}

$output = 'Sucessfully pulled park';
//include 'output.html.php';//

foreach ($result as $row)
{
$parkdetails[] = array(
'name' => $row['name'],
'blurb' => $row['blurb'],
'website' => $row['website'],
'address' => $row['address'],
'logo' => $row['logo']);
}

try
{
$park_id = $_GET['park_id'];
$query2="SELECT `name` FROM  `tpf_rides` WHERE  `park_id` = $park_id AND  

`top_ride` = 1 ORDER BY `name` ASC";
$result2 = $pdo->query($query2);
}
catch (PDOException $e)
{
$output = 'Unable to pull rides.';
include 'output.html.php';
}

$output = 'Sucessfully pulled rides';
//include 'output.html.php';//

try
{
$park_id = $_GET['park_id'];
$query3="SELECT `url` FROM  `tpf_images` WHERE  `park_id` = $park_id ORDER 

BY RAND() LIMIT 1";
$result3 = $pdo->query($query3);
}
catch (PDOException $e)
{
$output = 'Unable to pull image.';
include 'output.html.php';
}

$output = 'Sucessfully pulled image';
//include 'output.html.php';//


?>

这是结果显示的地方

<ul>
<?php foreach ($result2 as $row2): ?>

<li><h3><?php echo $row2['name']; ?></h3></li>

<?php endforeach; ?>
</ul>
<hr>

<?php

if ($count2 = $results2->rowcount() < 1)

{
    echo "No results found";
} 

else 

{
 echo "<h2>Top Attractions</h2>";

foreach ($result2 as $row2){

echo "<li><h3>{$row2['name']}</h3></li>";

}


}

?>

第133行现在是if ($count2 = $results2->rowcount() < 1)