PHP help with if else

PHP help with if else

am 27.08.2007 18:22:07 von Steve

Can anyone help me with this code. What I want to do is check the DB and if
there is no result returned (by checking the date field) I want to echo "no
shows".

But no matter what I do it seems to blow through ANY if else statements I try
and if there are dates it shows them if not it is just blank.

is it as simple as my code being screwed up or am i missing something else?

Steve


while($band = mysql_fetch_array($result, MYSQL_ASSOC))
{

$change_to_time = strtotime($band[date]);
$newdate = date('m.d.Y', $change_to_time);


if (empty($band[date])) {
echo "No shows on the calendar.";
}

else {
echo "

$newdate - $band[venue]
";
echo "";
echo "
$band[notes]
";
echo "
Time - $band[time], Cost - $band[cost]
";
echo "
";
}

}

Re: PHP help with if else

am 27.08.2007 18:42:44 von Jerry Stuckle

Steve wrote:
> Can anyone help me with this code. What I want to do is check the DB and if
> there is no result returned (by checking the date field) I want to echo "no
> shows".
>
> But no matter what I do it seems to blow through ANY if else statements I try
> and if there are dates it shows them if not it is just blank.
>
> is it as simple as my code being screwed up or am i missing something else?
>
> Steve
>
>
> while($band = mysql_fetch_array($result, MYSQL_ASSOC))
> {
>
> $change_to_time = strtotime($band[date]);
> $newdate = date('m.d.Y', $change_to_time);
>
>
> if (empty($band[date])) {
> echo "No shows on the calendar.";
> }
>
> else {
> echo "

$newdate - $band[venue]
";
> echo "
";
> echo "
$band[notes]
";
> echo "
Time - $band[time], Cost - $band[cost]
";
> echo "
";
> }
>
> }
>
>

You didn't show us your SELECT statement, but if it doesn't return any
rows, your mysql_fetch_array() call will return false - and not execute
anything in the loop.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: PHP help with if else

am 27.08.2007 18:52:25 von Steve

>
> You didn't show us your SELECT statement, but if it doesn't return any
> rows, your mysql_fetch_array() call will return false - and not execute
> anything in the loop.
>
>

Here it is with the select statement. The statement DOES work, because if
there are listings in the DB they will display correctly. It's just the
if...else portion of the code that i can not get to execute.

$sql = "select ID, date, venue, street, city, state, time, cost, notes from
tblBand order by date";
$result = mysql_query($sql);


while($band = mysql_fetch_array($result, MYSQL_ASSOC))
{
$change_to_time = strtotime($band[date]);
$newdate = date('m.d.Y', $change_to_time);


if (empty($band[date])) {
echo "No shows on the calendar.";
}

else {
echo "

$newdate - $band[venue]
";
echo "
";
echo "
$band[notes]
";
echo "
Time - $band[time], Cost - $band[cost]
";
echo "
";
}
}

Re: PHP help with if else

am 27.08.2007 18:55:07 von Steve

>
> You didn't show us your SELECT statement, but if it doesn't return any
> rows, your mysql_fetch_array() call will return false - and not execute
> anything in the loop.
>
>

You're right, I didnt think of the array returning false.

So how would i check for a value in the array, if the array returns false? Or
how would I check for ANY results?

Re: PHP help with if else - Got it!

am 27.08.2007 19:25:55 von Steve

I'm not checking for results with this -

if ($num_rows == 0) {
echo "No shows on the calendar.";
}


and it is now working.

Re: PHP help with if else

am 28.08.2007 02:15:46 von Jerry Stuckle

Steve wrote:
>> You didn't show us your SELECT statement, but if it doesn't return any
>> rows, your mysql_fetch_array() call will return false - and not execute
>> anything in the loop.
>>
>>
>
> You're right, I didnt think of the array returning false.
>
> So how would i check for a value in the array, if the array returns false? Or
> how would I check for ANY results?
>

if mysql_fetch_array() returns false, there are no (more) rows to retrieve.

You can check mysql_num_rows() to see how many rows are returned. Or
you can try to fetch the first row - if it doesn't work, put out a
default. If it does work, go into a loop (ensure you fetch the next row
at the end of the loop - not the beginning!).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: PHP help with if else

am 28.08.2007 19:19:16 von unknown

Post removed (X-No-Archive: yes)