I'm having trouble with getting my echo showing up. i think there is a problem with the while
but the table is in the mysql so it should work normally. This is my code
<?php
$sql = $db->prepare('SELECT
topic_id,
topic_subject
FROM
topics
WHERE
topics.topic_id = :topid');
$sql->bindParam(':topid', $_GET['id'], PDO::PARAM_INT);
$sql->execute();
$result = $sql->rowCount();
if($result === FALSE){
echo 'The topic could not be displayed, please try again later.';
}
elseif(count($result) === 0){
echo 'This topic doesn′t exist.';
}
else
{
while($row = $sql->fetch())
{
//display post data
echo '<table class="topic" border="1">
<tr>
<th colspan="2">' . $row['topic_subject'] . '</th>
</tr>'; ?>
the while
should show up because the topic exists in mysql. When i'm using avar_dump($sql->errorInfo());
it say's array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL }
The null is because i created the topic in mysql as a test.
$result = $sql->rowCount();
elseif(count($result) === 0){
$result is being assigned a row count; but count()
is designed to count the elements in an array. From the manual
If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned.
I think all you need is:
elseif($result === 0){
What I think's happening is that there are no results from your query, but the call to count()
isn't working. It's passing that check, and since there are no records to retrieve, it's not going into the loop.
count($results) === 0
doesn't make any sense, as $result
already contains an integer. Your SQL query returns 0 rows, but since count(0)
is 1
, your script does not know what to do, because the situation is not covered.
Just change count($results) === 0
with $results === 0
, and proceed to figure out why the query does not return the expected rows.