![]() |
MySQL's LIKE condition query
Hiya,
Am making a rating script and what I want is, when you have already voted in the particular rating bar, you cannot rate again, it just becomes static. In the database where I store the votes, values and IP's of the rating, in the IP row, all the IP's are serialized. Code:
a:1:{i:0;s:14:"81.102.225.228";}PHP Code:
PHP Code:
Anyone have an idea's? Thanks, - Chris. |
I would say you are doing this completely wrong...
Serializing things in a database should just be used to unserialize with php when you need to store arrays that you cannot simply store in the database. Generally (always) speaking you would never attempt to search do a query on a serialized column. You should store: IP Rating In seperate column then do: SELECT * FROM table WHERE ip = '192.168.0.1' Using LIKE doesnt scale well either, which is another reason this is better. If you do not understand this, please make another post and i'll try and explain a little better, or someone else will. If you really want your script to work you need to add a wildcard to the prefix aswell as the affix to the IP variable in the sql query: PHP Code:
'%'.$ip.'%', |
I'd also suggest not using an IP as the primary key, consider a large network such as a College/University, only one person per network can vote. I'd suggest limiting voting to registered members (assuming you have registered members).
|
What i normally do, is have it for both.
have in the mysql: id, rating, ip, user When a user logs in/signs up, it updates the ratings table to update the user column. This imo is the best approach for ratings as you do not force people to signup (which is tedious at the best of times) and handle registered users aswell. |
But how does that overcome the network problem? 1 person votes on a 500 computer network and the other 499 users can't vote.
|
I'm not sure how i did it now, but i think it allowed people from the same ip address to post multiple ratings if they were logged in. But it would also allow IP addresses to rate a game once if they are not logged in.
I hope that makes sense? |
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:
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:
PHP Code:
|
Ok I think I sorted it, I feel a bit dumb for not thinking about mysql_num_rows. Also I think there was a problem with the query, using sprintf, because I've tried it with a query on it's own and it's fine. With sprintf how do you determine numbers with decimals in them, like you'd find in an IP, because I don't think %d is the answer.
Anyway, I've sorted it out, thanks for the help :D You guys rock, it's nice knowing there's somewhere I can come for help. |
With regards to sprintf and IP addresses, they're just strings not numbers so you'd use %s.
|
...And just to tie this thread up. You may wish to consider taking the advice of the posts further up and not serializing the data. LIKE is a slow piece of functionality for MySQL. How about using the IP address in its own designated column and making it a unique primary key? That way once MySQL finds the IP it's looking for, it won't continue trawling the entire table to see if there's another one.
|
I'll definitely consider doing that method when I decide to write up a poll script I want to have.
As for my rating script, I have it all working now, I've tested it with other people and it all works great, and am pretty happy... I surprise myself sometimes with what I can achieve. Thanks again! |
It may work for now but i can guarantee you that using LIKE is not the method to use for this sort of thing. You cannot use a key on the column, and when this starts to increase in votes it will get bloody slow :)
|
| All times are GMT. The time now is 06:23 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0