The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
How do you check unique existance?
Yeah... I know I'm asking a lot of questions today...
I have a function which inserts a new entry into a database... but certain fields in this table are defined as UNIQUE... If I input something that is not unique, I get the following error... Code:
Database error in vBulletin 3.8.1: Invalid SQL: INSERT INTO rank_players (pName, pRegion, pTeam, pCount, pTotal, pAverage, pTopTotal, pTopAverage, pExt) VALUES ('Jaxel', '', '', '0', '0', '0', '0', '0', ''); MySQL Error : Duplicate entry 'Jaxel' for key 2 How would I go about for checking for existance before I attempt to insert into the database? I know I can do an SQL query to check for existance, but that would start adding too many queries. Is it possible to do this check, without doing a query? |
#2
|
||||
|
||||
You can try...
Code:
INSERT INTO rank_players (...) ON DUPLICATE KEY UPDATE (...) Code:
REPLACE INTO rank_players (...) EDIT: Just found this... Code:
INSERT INTO Timeline (name, ts) SELECT @name, @ts WHERE NOT EXISTS (SELECT 1 FROM Timeline WHERE name=@name AND ts = @ts); |
#3
|
|||
|
|||
Naw... that stuff wont work for me, as I'm trying to return a readable prompt to the user...
I KNOW there is a function to check existance, I've seen it before, I just cant remember what it was. |
#4
|
||||
|
||||
You use the mysql_affected_rows function after the INSERT INTO ... WHERE NOT EXISTS sample code. If 1 row is affected, you inserted properly. If 0 rows are affected, then it already existed and you have to display the prompt to the user.
|
#5
|
||||
|
||||
Use INSERT IGNORE INTO and then check affected rows with $db->affected_rows().
|
#6
|
|||
|
|||
Thanks Dismounted! That was exactly what I was looking for... I am now using the following code:
Code:
$db->query_write("INSERT IGNORE INTO ".TABLE_PREFIX."rank_players (pName, pRegion, pTeam, pCount, pTotal, pAverage, pTopTotal, pTopAverage, pExt) VALUES ('".$name."', '".$region."', '".$team."', '0', '0', '0', '0', '0', '".$ext."')"); $update = $db->affected_rows(); if ($update) { $output = 'The entry for player (<b>'.$name.'</b>) has just been added to the database'; } else { $output = '<font color="red">Unable to add (<b>'.$name.'</b>) - Player name already exists!</font>'; } return $output; Is there a function I can use to replace the following code in the same manner? Code:
$db->query_write("UPDATE ".TABLE_PREFIX."rank_players SET pName='".$name."' WHERE playerID='".$player['playerID']."'"); $db->query_write("UPDATE ".TABLE_PREFIX."rank_players SET pRegion='".$region."' WHERE playerID='".$player['playerID']."'"); $db->query_write("UPDATE ".TABLE_PREFIX."rank_players SET pTeam='".$team."' WHERE playerID='".$player['playerID']."'"); $db->query_write("UPDATE ".TABLE_PREFIX."rank_players SET pExt='".$ext."' WHERE playerID='".$player['playerID']."'"); |
#7
|
||||
|
||||
You can also use the IGNORE keyword in UPDATE queries. It is just UPDATE IGNORE.
|
#8
|
|||
|
|||
Thanks... you're really helping... there is one more query I am trying to figure out...
Code:
SELECT rank_players.*, SUM(sValue) as total FROM rank_players INNER JOIN rank_scores USING(playerID) GROUP BY playerID ORDER BY total DESC, pName ASC LIMIT $limit,$rebcount However, there is one thing I'm failing at figuring out... The SUM(sValue) string... I want it to ONLY count the sum of values... Code:
SELECT * FROM rank_scores INNER JOIN rank_events USING(eventID) WHERE rank_events.eDate > $cutoff LIMIT $bestcount |
#9
|
||||
|
||||
What's $bestcount?
|
#10
|
|||
|
|||
$bestcount is 10...
Basically, I only want to SUM the best 10 scores that a player has... normally I would just put the LIMIT in the first statement... but I am already using that limit for the start/end page refresh. |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|