If Rows Returned = 0 (MySQL)

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
Tim1681
-


Joined: 17 Jan 2005
Posts: 160
Location: Bristol, CT, USA

PostPosted: Sun Jul 24, 2005 2:35 am    Post subject: If Rows Returned = 0 (MySQL) Reply with quote

Kind of a newbie question but i havent tried this yet. I have a part in a website im making where it displays Events that are happening on the Current Date, how would i tell it when the Rows Returned are = 0 to echo "No Events Today? I've Tried
Code:
if($row['name'] == "")

but that apparently wont work. Heres the full code that i tried.
Code:
$query = "SELECT * FROM upcoming_shows WHERE date = CURDATE() ORDER BY name";
$result = mysql_query($query)
  or die("Query Failed: ".mysql_error());
while ($row = mysql_fetch_array($result))
 {
  if($row['name'] == "")
   {
   echo "<tr><td>No Events Today</td></tr>";
   }
  else
   {
    echo "<tr><td valign='center' style='border-bottom: solid 1px #ff0000;'><a href='../events.php?e=", $row['name'], "' target='_parent'>", $row['name'], "</a></td><td align='right' style='border-bottom: solid 1px #ff0000;'>", $row['date_read'], "</td></tr>";
    }
 }
Back to top View user's profile Send private message AIM Address
k1ll3rdr4g0n
-


Joined: 04 Jul 2004
Posts: 609

PostPosted: Sun Jul 24, 2005 5:08 am    Post subject: Reply with quote

Try this:

Code:
if(mysql_affected_rows()==0){
print 'nothing today';
}

_________________
Back to top View user's profile Send private message AIM Address
Tim1681
-


Joined: 17 Jan 2005
Posts: 160
Location: Bristol, CT, USA

PostPosted: Sun Jul 24, 2005 3:06 pm    Post subject: Reply with quote

Still isn't working. What makes me mad is that when there is actually an event it shows the text for the event thats happaning, but when there is no event for that day it doesnt show the "No Events" that i tell it to. I even moved the script around to see if it was a different problem but it still worked fine except for the "No Events".

Code:
while ($row = mysql_fetch_array($result))
{
 if(mysql_affected_rows() !== 0)
  {
   echo "<tr><td>EVENT</td></tr>";
  }
 else
  {
   echo "<tr><td>No Events Today</td></tr>";
  }
}
Back to top View user's profile Send private message AIM Address
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sun Jul 24, 2005 3:58 pm    Post subject: Reply with quote

Code:
if (mysql_num_rows($sql_query_variable) > 0) {
//Events
} else {
echo "No events.";
}


Im back after my lan went boom =S
_________________


Last edited by MonkeyNation on Sun Jul 24, 2005 3:59 pm; edited 1 time in total
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
p3
-


Joined: 17 Jun 2005
Posts: 615

PostPosted: Sun Jul 24, 2005 3:59 pm    Post subject: Reply with quote

MonkeyNation wrote:
Code:
if (mysql_num_rows > 0) {
//Events
} else {
echo "No events.";
}


Im back after my lan went boom =S

Good to see you back, MonkeyNation.
Back to top View user's profile Send private message Send e-mail
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sun Jul 24, 2005 4:00 pm    Post subject: Reply with quote

p3 wrote:
MonkeyNation wrote:
Code:
if (mysql_num_rows($sql_query_variable) > 0) {
//Events
} else {
echo "No events.";
}


Im back after my lan went boom =S

Good to see you back, MonkeyNation.


10 seconds after my first post I get welcomed back =)
_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
k1ll3rdr4g0n
-


Joined: 04 Jul 2004
Posts: 609

PostPosted: Sun Jul 24, 2005 4:00 pm    Post subject: Reply with quote

Hmm...it works in my script (for a login script) try...

Code:
$query = "SELECT * FROM upcoming_shows WHERE date = CURDATE() ORDER BY name";
$result = mysql_query($query)  or die("Query Failed: ".mysql_error());
if(mysql_affected_rows()==0){
echo 'oh nothing today';
} else { while  ($row = mysql_fetch_array($result))
{
    echo "<tr><td valign='center' style='border-bottom: solid 1px #ff0000;'><a href='../events.php?e=", $row['name'], "' target='_parent'>", $row['name'], "</a></td><td align='right' style='border-bottom: solid 1px #ff0000;'>", $row['date_read'], "</td></tr>"; 
}
}


That should work..If not you wanna send over a sample of your mysql table. :).
_________________
Back to top View user's profile Send private message AIM Address
p3
-


Joined: 17 Jun 2005
Posts: 615

PostPosted: Sun Jul 24, 2005 4:01 pm    Post subject: Reply with quote

MonkeyNation wrote:
p3 wrote:
MonkeyNation wrote:
Code:
if (mysql_num_rows($sql_query_variable) > 0) {
//Events
} else {
echo "No events.";
}


Im back after my lan went boom =S

Good to see you back, MonkeyNation.


10 seconds after my first post I get welcomed back =)

:)
Back to top View user's profile Send private message Send e-mail
Tim1681
-


Joined: 17 Jan 2005
Posts: 160
Location: Bristol, CT, USA

PostPosted: Sun Jul 24, 2005 4:04 pm    Post subject: Reply with quote

Thnx k1ll3rdr4g0n works like a charm =)
Back to top View user's profile Send private message AIM Address
k1ll3rdr4g0n
-


Joined: 04 Jul 2004
Posts: 609

PostPosted: Sun Jul 24, 2005 4:15 pm    Post subject: Reply with quote

Tim1681 wrote:
Thnx k1ll3rdr4g0n works like a charm =)


:D, your welcome.
_________________
Back to top View user's profile Send private message AIM Address
Tim1681
-


Joined: 17 Jan 2005
Posts: 160
Location: Bristol, CT, USA

PostPosted: Wed Jul 27, 2005 3:23 am    Post subject: Reply with quote

Another Question..

I have a Marquee on my Index page of this site, and the MySQL Select Query is:
Code:
$query = "SELECT * FROM upcoming_shows WHERE date = CURDATE() ORDER BY name";


This only selects the entries where the date is equal to the server's CURDATE(). Now the question, there is an Event where it runs for 2 dates, 2005-08-06 and 2005-08-07 .. If i entered:
Code:
2005-08-06, 2005-08-07

into the MySQL table under the Date category, would it still be read by PHP for both dates? Or is there some other Syntax i should be using which does the same as what i'm trying to achieve?
Back to top View user's profile Send private message AIM Address
k1ll3rdr4g0n
-


Joined: 04 Jul 2004
Posts: 609

PostPosted: Wed Jul 27, 2005 5:15 am    Post subject: Reply with quote

try...

Code:
$query = "SELECT * FROM upcoming_shows WHERE date = 2005-08-06, 2005-08-07 ORDER BY name";

_________________
Back to top View user's profile Send private message AIM Address
Tim1681
-


Joined: 17 Jan 2005
Posts: 160
Location: Bristol, CT, USA

PostPosted: Wed Jul 27, 2005 12:28 pm    Post subject: Reply with quote

Well the problem is that it isnt just THAT event, theres a whole list of them. I set it up so there is a Marquee which displays wether there is an event that day ( WHERE date = CURDATE() ), and below it is a table with all the Upcoming Events ( WHERE date > CURDATE() ). Thats why im having the problem.

I was hoping that if I used the "," that PHP would Read each date seperately and it would be placed in the Marquee on both dates. I havent tried it yet but I will today, but before I do, do u have any other ideas that u believe might work? :D
Back to top View user's profile Send private message AIM Address
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Wed Jul 27, 2005 6:18 pm    Post subject: Reply with quote

Make 2 rows; 1 for each date.
It will undoubtably appear in upcoming events too, but you could easily put an
Code:
if ($row['date'] !== "1/2/3") { //Show mysql result }

as a temporary solution.
Or if you plan to do this more often youll have to add another column called 'extended' or something, and if its a multidate marquee then make 'extended' 1 or something and make a check that $row['extended'] isnt 1 before it shows upcoming events.
_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
Tim1681
-


Joined: 17 Jan 2005
Posts: 160
Location: Bristol, CT, USA

PostPosted: Wed Jul 27, 2005 8:32 pm    Post subject: Reply with quote

Yea, I will probably just make 2 Rows in the table. Thnx for all the help.
Back to top View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group