Page 1 of 1
need mysql proof read help...advice appreciated
Posted: Tue Jul 28, 2009 7:51 pm
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);
}
Re: need mysql proof read help...advice appreciated
Posted: Tue Jul 28, 2009 8:22 pm
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".)
Re: need mysql proof read help...advice appreciated
Posted: Tue Jul 28, 2009 8:35 pm
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?
Re: need mysql proof read help...advice appreciated
Posted: Tue Jul 28, 2009 11:20 pm
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.)
Re: need mysql proof read help...advice appreciated
Posted: Tue Jul 28, 2009 11:39 pm
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.
Re: need mysql proof read help...advice appreciated
Posted: Wed Jul 29, 2009 12:22 am
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.
Re: need mysql proof read help...advice appreciated
Posted: Wed Jul 29, 2009 8:13 pm
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.
Re: need mysql proof read help...advice appreciated
Posted: Fri Jul 31, 2009 10:22 pm
by scarface222
anyone have any idea? Tasairis?
Re: need mysql proof read help...advice appreciated
Posted: Sat Aug 01, 2009 6:54 am
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?
Re: need mysql proof read help...advice appreciated
Posted: Thu Aug 06, 2009 8:11 pm
by scarface222
I have tried that and it is still undefined. Does anyone see any errors with my code that could make $count undefined?
Re: need mysql proof read help...advice appreciated
Posted: Fri Aug 07, 2009 12:32 am
by scarface222
up for the last time. If anyone has an idea please let me know if not thanks anyway everyone...