Jump to content

A check to see if a query will return anything ?


j33baS

Recommended Posts

Hi I've spent ages googling this but cant find anything which helps so naturally I came here !

 

so let me explain ... I have a player profile page for a basketball site which will display the types of points scored by a player. Lets say I have two tables, one holding info(player name, number scored and opposition) on 3pointers and one for 2pointers

 

3 pointers

jack - 5 vs reds

jack - 3 vs blues

jack - 4 vs greens

Gill - 6 vs reds

Gill - 4 vs blues

 

2 pointers

Gill - 9 vs reds

Gill - 8 vs blues

 

The default profile page will have to contain queries for both the 2pointers and 3pointers tables.  But Because jack hasn't scored and 2 pointers, his profile page will display the 3pointers he has scored, but the 2pointers bit will just be blank, I'd like it to say sumthing like "no 2 pointers scored yet"

 

So im basically my code says...

 

$sql "SELECT * FROM 2points WHERE id = $id";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
echo "...output scores, names etc";
}

 

and the same goes for the 3pointers table ....

 

but what im trying to do is more along the lines of ...

 

if (this query exists){
$sql "SELECT * FROM 2points WHERE id = $id";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
...output scores, names etc
}
}
else{
echo "nothing scored yet";
}

 

can anyone help me here? the codes not the problem because im getting the ouputs i want, its just the error handling, so I want "nothing scored yet" if the query is going to be empty

 

cheers!

 

Link to comment
Share on other sites

what about this?

 

if (this query exists){
$sql "SELECT * FROM 2points WHERE id = $id";
$result = mysql_query($sql);
$rows = mysql_num_rows($result)

if ($rows == 0)
{ 
echo "nothing scored yet";
}

else { while ($row = mysql_fetch_array($result)){

...output scores, names etc
}
}
}

Link to comment
Share on other sites

hey, thanks for helping!

 

I already tried that ... Im getting an error sayig that theres an "Unexpected T_ELSE"

 

but thats the path i was wanting to go down!

 

Any reasons as to why it might be producing that error ? its not a problem having a while loop nested within the if ?

 

cheers

Link to comment
Share on other sites

$sql "SELECT * FROM 2points WHERE id = $id";
$result = mysql_query($sql);
$row = mysql_num_rows($result);
if($row == 0){
echo "nothing scored";
}
else{
while ($row = mysql_fetch_array($result)){
echo "scores names etc";
}
}

 

ok sorry, i did miss a bracket! codes running fine now, except im not getting the output i want !

 

"Jacks" 2point query still displays nothing and not the text "nothing scored" !

 

and Gills querys both display the info fine.

 

Thanks for the help guys, any more suggestions ?

Link to comment
Share on other sites

<?php
$sql "SELECT * FROM 2points WHERE id = $id";
$result = mysql_query($sql);

if(empty($result)||$result==""){die('there was an error with the query!');}

$row = mysql_num_rows($result);
if($row =="0"){
echo "nothing scored";
}
else{
while ($row = mysql_fetch_array($result)){
echo "scores names etc";
}
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.