0

I'm using an if statement in PHP whereby if a query is empty it doesn't run the PHP effectivily hiding that part of the web page. This is the if statement I'm using

if(!empty($result4)){

}

It works fine elsewhere when used to hide individual empty rows from a whole result but when run for a whole query it doesn't work.

This is the whole php code everything below if(!empty($result4)){ should not happen if query is empty, but it is.

<?php 
if(!empty($result4)){


printf('<h2>%s News' . PHP_EOL, $row['name']);



$sLastStory = '';
foreach ($result4 AS $row4)
{
    $sStory = $row4['headline'] . $row4['story'];
    if (strcasecmp($sStory, $sLastStory) != 0)
    {
        if (!empty($sLastStory))
        {

        }
        $sLastStory = $sStory;


        printf('<h3>%s</h3>' . PHP_EOL, $row4['headline']);

        printf('<h4>%s</h4>' . PHP_EOL, $row4['Displaydate']);

        printf('<p>%s</p>' . PHP_EOL, $row4['story']);
    }

if(!empty($row4['url'])){   
printf('
<a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s - Credit - %s" >
<img src="/images/%s%s-thumb.jpg" style="max-height: 230px; max-width: 230px" alt="%s"/></a>' . PHP_EOL, $row4

['url'], $row4['alt'], $row4['headline'],  $row4['description'],$row4['credit'], $row4['url'], $row4['alt'], 

$row4['alt'] );
}

}

printf('

<br>

<hr>
<a class="bloglink" href="parknews.php?park_id=%s">See all %s news</a></li>' . PHP_EOL, $park_id, $row

['name']);

}
?>

Any ideas how to make it work?

If it helps this is the MySQL query:

$park_id = $_GET['park_id'];
$query4= 'SELECT headline, story, DATE_FORMAT(date, "%d-%M-%Y") AS Displaydate, url, alt, description, credit
FROM tpf_news 
LEFT JOIN tpf_images ON tpf_news.news_id = tpf_images.news_id 
Where tpf_news.park_id = ' .  $park_id .' ORDER BY date DESC';
$result4 = $pdo->query($query4);

Thanks

1
  • What do you mean by an empty result? No rows returned? Commented May 21, 2013 at 15:33

6 Answers 6

3

If the query succeeded, PDO::query() will return a PDOStatement even if no rows were selected. Since $result4 contains a PDOStatement, it is not empty.

A better way to check the number of returned rows is PDOStatement::rowcount:

if( $result4->rowCount() ){
Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly. It's my fault I'm really new to PHP but now I know how to use rowCount() Thanks
1

empty should not work here. If your sql statement returns no result, the PDO::query method return an object anyway : a PDOStatement. But you can use rowCount to see if your statement is empty.

Comments

1

There might be a lot of choices here to empty()

empty() Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

to overcome this issue you need to check the number of rows returned from the query because if it has no errors it will return true

rowCount();

Check it out here

http://php.net/manual/en/pdostatement.rowcount.php

I hope this can help :)

Comments

1

A result of a query will never be empty, also if there are no rows matched. It could be either null or have anything else, so your condition if(!empty($result4)) will be never satisfied. You should check for num_rows returned instead

if($result4->rowCount() > 0)

1 Comment

You need to clarify your explanation. The result of a query can be empty (no rows) but the PDOStatement object returned will never satisfy empty() != false
0

What exactly do you understand the empty() function to do? I guess you think it should tell you whether the query has any results or not. That is not what it does.

What empty() does is return true if the variable (in this case $result4) is null or contains some other "empty" value.

A recordset object is not an "empty" value, regardless of how many records it got.

What you probably want to use instead for this case is

if($result4->rowCount() > 0)

See the PHP manual for PDOStatement::rowCount.

empty() would only be useful here if you expected the query to fail completely -- eg an invalid query or broken DB connection. In that case, $result4 would be null. But you'd also have PDO errors that you'd use to deal with that rather than checking for the recordset object being empty.

Hope that helps.

Comments

0

Try this

if (is_array($result4) && sizeof($result4)>0) {
    // your code here!
}

or

$query4 = 'SELECT headline, story, DATE_FORMAT(date, "%d-%M-%Y") '
    . ' AS Displaydate, url, alt, description, credit '
    . ' FROM tpf_news '
    . ' LEFT JOIN tpf_images '
    . ' ON tpf_news.news_id = tpf_images.news_id '
    . ' Where tpf_news.park_id = ? '
    . 'ORDER BY date DESC';

$st = $pdo->prepare($query4);

// The following code can be placed in a loop

$st->execute(array($_GET['park_id']));

if ($st->rowCount()>0) {
    $aResult = $st->fetch();
    // your code

}

Marcos

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.