need mysql proof read help...advice appreciated

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

need mysql proof read help...advice appreciated

Post by scarface222 »

Hey guys I have this awkward function and I am sure the syntax is not right because I get different errors once in a while. For example I get that $count is undefined...How is this possible... The function's goal is to print the users online on each page that is listed. I was just wondering if someone could proof it for me.

Code: Select all

function usersOnline($pgcod,$td)
{
    $qryuserscount="SELECT * FROM scrusersonline WHERE usersonlineusrip!='1' AND td='$td'";
$userscount=mysql_query($qryuserscount);
while ($row = mysql_fetch_assoc($userscount)) {
 $ip= $row['usersonlineusrip'];
$qryuserscount1="SELECT * FROM active_users WHERE ip='$ip'";
$userscount1=mysql_query($qryuserscount1);
$count = mysql_num_rows($userscount1);
}
        
$pageuserscount=usersCount($pgcod,$td);
echo "$pageuserscount Total viewer(s) online on this page and $count User(s) online on this page";
}
$first_query="SELECT * FROM topic WHERE sd ='1'";
$result=mysql_query($first_query) or die('Error, select query failed');
while ($row = mysql_fetch_assoc($result)) {
    $tc= $row['te'];
    $td= $row['td'];
    echo "<br><a href=\"ce.php?td=$td\">$tc</a>";echo usersOnline(1,$td);
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: need mysql proof read help...advice appreciated

Post by requinix »

Well, you assign $count in a loop, so if $count is undefined then the loop never ran...

After you realize why that could happen, change your function to return a string, not echo it directly. (Just change that one "echo" to "return".)
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: need mysql proof read help...advice appreciated

Post by scarface222 »

Thank you for the advice however I did what you said and $count is still undefined (changed echo to return)...maybe I misunderstood... how could the loop not run that's what I cannot understand. For example if the database returns no results shouldn't $count still be defined just as a blank space?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: need mysql proof read help...advice appreciated

Post by requinix »

scarface222 wrote:For example if the database returns no results shouldn't $count still be defined just as a blank space?
Why would it be? There's no code that says that should happen.

PHP isn't psychic: it won't notice that there was a variable left undefined simply because some code didn't run. For all it knows, you planned it that way.
(Except you didn't.)
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: need mysql proof read help...advice appreciated

Post by scarface222 »

You got any idea what I should do with regards to this problem the script? Sorry man I am kind of new to php so I have a somewhat limited understanding. I appreciate your help thus far. And also have you ever launched a site before? I asked because you seem experienced. I know its off topic but I am in the process of doing so and would appreciate any advice. I am planning on consulting a lawyer for copyright, trademark, and a disclaimer contract but do not have a final decision for a service provider or ad provider or idea of what I will need for monitoring and maintenance in the future.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: need mysql proof read help...advice appreciated

Post by requinix »

No sites run by myself, no.

What is $count supposed to track? Putting it in the loop there means it will only have the value from the last user. If you want a total number then you should be adding all the counts together: that means setting $count=0 before the loop and adding onto it each time.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: need mysql proof read help...advice appreciated

Post by scarface222 »

What i was meaning to do was for each entry where usersonlineusrip!='1' (does not equal 1 and will be equal to the user's ip) and td=$td (represents unique page) I want to count the total for the page in question. So for each value of td I want the total count of entries not equal to 1. So should I still just set $count equal to 0 before counting the rows? Will that achieve the result I need? Thanks again.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: need mysql proof read help...advice appreciated

Post by scarface222 »

anyone have any idea? Tasairis?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: need mysql proof read help...advice appreciated

Post by cpetercarter »

If $count is undefined, it is because the loop did not run. And if the loop did not run, it is probably because the database query returned no records.
Why not set $count = 0; before you run the loop?
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: need mysql proof read help...advice appreciated

Post by scarface222 »

I have tried that and it is still undefined. Does anyone see any errors with my code that could make $count undefined?
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: need mysql proof read help...advice appreciated

Post by scarface222 »

up for the last time. If anyone has an idea please let me know if not thanks anyway everyone...
Post Reply