View Single Post
Old 10-24-2007, 02:38 PM   #7 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Going right back to the first post, you said that you can vote even if you've already voted before. This is not a SQL problem, it's a PHP problem. You have some code which you want to assign a boolean (true/false) value to the variable $voted but the way you go about it will not give false if the user has not voted:
PHP Code:
$voted mysql_query(...); 
Take a look at the PHP manual for mysql_query() and you'll see that the function returns either a resource, TRUE or FALSE in different circumstances. Only if the SQL statement causes an error will the function return false. If no rows are returned from the SQL query, the $voted variable will NOT be set to false! It will be set to a resource, regardless of the number of rows returned. Therefore, you can't simply check for true/false in the if statement -- you'd need to check if any rows were returned, with the mysql_num_rows() function.

To be able to use the if statement properly, you would need to change the assignment of the $voted variable to something more along the lines of:
PHP Code:
$voted mysql_query(...) or die(mysql_error());
$voted = (bool) mysql_num_rows($voted) > 0;
// $voted now will be either true (1 or more rows returned) or false (zero rows) 
Also, since you're not really interested in the row(s) returned from the database then why return rows/columns when you don't need to?
PHP Code:
SELECT
    
'yay, found the ip' as result
FROM 
    mytable
WHERE 
    ip LIKE 
'%127.0.0.1%'
    
AND id 1
LIMIT 
    0
,1
Salathe is offline  
Reply With Quote