Error please help

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
anrob
Forum Newbie
Posts: 3
Joined: Fri Sep 20, 2002 3:00 pm

Error please help

Post by anrob »

I have no idea why this wont work. ====NEWBIE====


Here is the Error

Parse error: parse error, unexpected T_STRING in /home/teenvote/public_html/user.php on line 496


Here is the Code

Code: Select all

// New stuff -->
//$table_name = "select * from usertable";
$time = time(); 
mysql_query("update $usertable set ip = "$REMOTE_ADDR" where name = "$name" "); 
mysql_query("update $usertable set timeout = $time where name = "$name" ");
// here's the meat. 
// be sure to edit the $seconds var. if you want to change the timeout. 
// $seconds is currently set to one hour.  
function usersOnline() { 

    $query = mysql_query("select ip,timeout from $usertable) or die(mysql_error()); 
    $result = mysql_query($query); 
    $numrows = mysql_num_rows($result); 
    if($numrows == 0) { 
        echo '<b>'no'</b>' users online'; 
    } elseif($numrows <= 1) { 
        echo '<b>' . $numrows . '</b> user online'; 
    } else { 
        echo '<b>' . $numrows . '</b> users online'; 

    $result1 = mysql_query($query) or die(mysql_error()); 
    while($row = mysql_fetch_array($result1)) { 
    $timeout = $row{ї'timeout']} + 3600 <= time(); 
    $seconds = 3600; 
    $timestamp = time(); 
    $result2 = $timestamp - $seconds; 
        if($timeout) { 
            mysql_query("update $usertable set ip = "0" ")or die(mysql_error()); 
        } 
    } 
}

Thanks
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

is the line number you're getting the error on this statement?

Code: Select all

<?php
mysql_query("update $usertable set ip = "$REMOTE_ADDR" where name = "$name" "); 

?>
anrob
Forum Newbie
Posts: 3
Joined: Fri Sep 20, 2002 3:00 pm

This Line

Post by anrob »

It is on this line

Code: Select all

mysql_query("update $usertable set ip = "0" ")or die(mysql_error());
Here is the original code, if this helps

Code: Select all

function usersOnline() { 

    $query = "select ip, timeout from $table_name where ip is not null and timeout is not null"; 
    $result = mysql_query($query); 
    $numrows = mysql_num_rows($result); 

    if($numrows == 0) { 
        echo "<b>no</b> users online"; 
    } elseif($numrows <= 1) { 
        echo "<b>" . $numrows . "</b> user online"; 
    } else { 
        echo "<b>" . $numrows . "</b> users online"; 
    } 

    $result1 = mysql_query($query); 
    while($row = mysql_fetch_array($result1)) { 
    $timeout = $rowї"timeout"] + 3600 <= time(); 
    $seconds = 3600; 
    $timestamp = time(); 
    $result2 = $timestamp-$seconds; 
        if($timeout) { 
            mysql_query("update $table_name set ip = NULL where timeout < $result2"); 
        } 
    } 
}
By the way thanks for answering so fast.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

try this

Code: Select all

<?php
mysql_query("update $usertable set ip = 0 ")or die(mysql_error()); 

?>
anrob
Forum Newbie
Posts: 3
Joined: Fri Sep 20, 2002 3:00 pm

Post by anrob »

JPlush76 wrote:try this

Code: Select all

<?php
mysql_query("update $usertable set ip = 0 ")or die(mysql_error()); 

?>
Still have the same error

Parse error: parse error, unexpected T_STRING in /home/teenvote/public_html/user.php on line 496

Somehow if i remove that entire section of code the script works fine.

It is trying to update the ip address to null after a timeout of 1` hour
it read the ip and adds everything but when it get to the part about null
for the ip add everything goes wrong.

Thanks
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Code: Select all

<?php
// New stuff --> 
//$table_name = "select * from usertable"; 
$time = time(); 
mysql_query("update $usertable set ip = "$REMOTE_ADDR" where name = "$name" "); 
mysql_query("update $usertable set timeout = $time where name = "$name" "); 
// here's the meat. 
// be sure to edit the $seconds var. if you want to change the timeout. 
// $seconds is currently set to one hour. 
function usersOnline() { 

$query = mysql_query("select ip,timeout from $usertable) or die(mysql_error()); 
$result = mysql_query($query); 
$numrows = mysql_num_rows($result); 
if($numrows == 0) { 
echo '<b>'no'</b>' users online'; 
} elseif($numrows <= 1) { 
echo '<b>' . $numrows . '</b> user online'; 
} else { 
echo '<b>' . $numrows . '</b> users online'; 

$result1 = mysql_query($query) or die(mysql_error()); 
while($row = mysql_fetch_array($result1)) { 
$timeout = $row{ї'timeout']} + 3600 <= time(); 
$seconds = 3600; 
$timestamp = time(); 
$result2 = $timestamp - $seconds; 
if($timeout) { 
mysql_query("update $usertable set ip = "0" ")or die(mysql_error()); 
} 
} 
}
}
?>
User avatar
jonsyd
Forum Commoner
Posts: 36
Joined: Fri Sep 20, 2002 9:28 am
Location: Oxford, UK

Post by jonsyd »

Hi Takuma,

Please don't take this the wrong way, but just posting a complete block of code without any explanation of what it refers to or how it fixes things doesn't really help anyone.

Just as unhelpful, the code you posted won't run. It's littered with basic syntax errors which even the synax-colouring has picked up. I haven't bothered to go through it, but I can see a bunch of quote-related errors.

Can I suggest that when you post a block of code, you run it first to make sure its valid, or at the very least use the [Preview] button on the posting form so you can check it with syntax coloring.

Regards, Jonny
User avatar
jonsyd
Forum Commoner
Posts: 36
Joined: Fri Sep 20, 2002 9:28 am
Location: Oxford, UK

Post by jonsyd »

Try this:

Code: Select all

$time = time(); 
mysql_query("UPDATE $usertable SET ip = '$REMOTE_ADDR' WHERE name = '$name' "); 
mysql_query("UPDATE $usertable SET timeout = $time WHERE name = '$name' "); 

function usersOnline() {

    $query = "SELECT ip, timeout FROM $usertable ";
    $result = mysql_query($query) or die(mysql_error());
    $numrows = mysql_num_rows($result);
    
    if($numrows == 0) {
        echo '<b>no</b> users online';
        }
    
    elseif($numrows <= 1) {
        echo '<b>' . $numrows . '</b> user online';
        }
    
    else {
        echo '<b>' . $numrows . '</b> users online';
        $result1 = mysql_query($query) or die(mysql_error());
        while($row = mysql_fetch_array($result1)) {
            $timeout = $rowї'timeout'] + 3600 <= time();
            $seconds = 3600;
            $timestamp = time();
            $result2 = $timestamp - $seconds;
            if($timeout) {
                mysql_query("UPDATE $usertable SET ip = 0 ") or die(mysql_error());
                }
            }
        }
    }
Corrections:

* Missing } after while() statement

* echo '<b>'no'</b>' users online'; generates a parse error

* see if you can work out what was wrong with:
$query = mysql_query("select ip,timeout from $usertable) or die(mysql_error());
$result = mysql_query($query);

* $row['timeout'] rather than $row{['timeout']}

* Have a look at the queries: SQL keywords in uppercase and variables enclosed with ' rather than \" for legibility (or without anything for numbers).

Hope this helps, Jonny
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

One point to add to jonsyd's pretty comprehensive post:
This line:

Code: Select all

$result1 = mysql_query($query) or die(mysql_error());
can be deleted and

Code: Select all

while($row = mysql_fetch_array($result1)) {
can be changed to

Code: Select all

while($row = mysql_fetch_assoc($result)) {
since you've already queried the database using the same SQL query and haven't done anything with the result set except count how many records there are. Using mysql_fetch_assoc() means you only return an associative array. mysql_fetch_array() has both an associative and numerical index.

Mac
Post Reply