1

我正在运行这个查询

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 第 133 行“if ($count2 = $query2->rowcount() < 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

4 回答 4

1

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

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

奇怪的是,rowCount 是从 $pdo->query(); 返回的对象的函数,这似乎是一个内部故障。

尝试:

var_dump(get_class_methods($result)); 

看看我的意思。

于 2013-10-12T01:17:54.303 回答
0

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

$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";
    }
}

您将不得不更改您的查询以获取park_top字段,所以

$query2="SELECT `name` FROM  `tpf_rides` WHERE  `park_id` = $park_id AND  `top_ride` = 1 ORDER BY `name` ASC";
于 2013-04-13T11:55:30.790 回答
0

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

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

不是

if ($count2 = $query2->rowcount() < 1)
于 2013-04-13T11:30:26.953 回答
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)

于 2013-04-13T12:22:05.077 回答