Trouble with if numrows = 0 inside a loop

<?php include("headcode.php"); 


$thiscat = $_GET['catlist'];
$portstring = "SELECT port_id,port_title,port_date,port_summary,port_thumbnail,port_category FROM tbl_portfolio WHERE port_category='".$thiscat."' ORDER BY port_date DESC";
$portresults = mysql_query($portstring);
?>

<?php while($row = mysql_fetch_array($portresults)) {if ($row['port_id']==NULL) {echo "<p>There aren't any of those ".$thiscat." things here just yet.</p>";} else {

echo "<div class=\\"portlist portitem".$row['port_id']."\\"><a href=\\"details.php?id=".$row['port_id']."\\"><img src=\\"images/thumbs/".$row['port_thumbnail']."\\"></a><h3><a href=\\"details.php?id=".$row['port_id']."\\">".$row['port_title']."</a></h3><p>(".$row['port_summary'].")</p></div>";}}

?>

i want to make it so if there’s no records with $thiscat it displays that thing there… the if statement i have is not cutting it… i’m guessing it has something to do with the loop, but i’m not really clear on this stuff well on how to figuer this out

tried if (mysql_num_rows($row) == 0) instead of the null thing there and it seems like the row hasn’t been defined yet bc it always comes up as true

You can do it like this. I think this will help you.

<?php include("headcode.php");

$thiscat = $_GET['catlist'];
if(isset($thiscat))
{
$portstring = "SELECT port_id,port_title,port_date,port_summary,port_thumbnail,port_category FROM tbl_portfolio WHERE port_category='".$thiscat."' ORDER BY port_date DESC";
$portresults = mysql_query($portstring);
?>

<?php while($row = mysql_fetch_array($portresults)) {if ($row['port_id']==NULL) {echo "<p>There aren't any of those ".$thiscat." things here just yet.</p>";} else {

echo "<div class=\\"portlist portitem".$row['port_id']."\\"><a href=\\"details.php?id=".$row['port_id']."\\"><img src=\\"images/thumbs/".$row['port_thumbnail']."\\"></a><h3><a href=\\"details.php?id=".$row['port_id']."\\">".$row['port_title']."</a></h3><p>(".$row['port_summary'].")</p></div>";}}
}
?>

If there aren’t any rows, it won’t even get into that while loop.

<?php
include("headcode.php");
$thiscat = $_GET['catlist']
$thiscatSQL = mysql_real_escape_string($thiscat); //you MUST escape data, or people can execute SQL code through your URL!
$thiscatHTML = htmlentities($thiscat); //escaping for HTML output, just in case their query has HTML code in
$portstring = "SELECT
    port_id
    , port_title
    , port_date
    , port_summary
    , port_thumbnail
    , port_category
FROM
    tbl_portfolio
WHERE
    port_category='{$thiscatSQL}'
ORDER BY
    port_date DESC
"; //Formatting MySQL code - its a nice habbit to get into.
$portresults = mysql_query($portstring);
if($portresults === false || mysql_num_rows($portresults)){
    echo "<p>There aren't any of those {$thiscat} things here just yet.</p>";
}
while($row = mysql_fetch_array($portresults)){ //doesn't the following look much nicer when it's formatted? :)
    $ID = (int)$row['port_id'];
    $Thumb = $row['port_thumbnail'];
    $Title = htmlentities($row['port_title']); //stops any tags interfering with the heading format
    $Summary = $row['port_summary']; //no html escaping, allows links etc in the summary
    echo <<<OUTPUT
    <div class="portlist portitem{$ID}">
        <a href="details.php?id={$ID}">
            <img src="images/thumbs/{$Thumb}">
        </a>
        <h3>
            <a href="details.php?id={$ID}">
                {$Title}
            </a>
        </h3>
        <p>({$Summary})</p>
    </div>
OUTPUT;
}

?>

I’ve also tidied things up a bit. Scrap the formatting if you wish, but I’d recommend that you write neater code, it is much easier to maintain that way.