Author Topic: mysql question  (Read 536 times)

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
mysql question
« on: July 08, 2011, 07:16:12 AM »
"Select * from UserItems join Items on (UserItems.ItemID = Items.ItemID) "

Does anything see anything wrong with this query?  I believe Items might be a reserved keyword.

For UserItems, I have:

UserID  |   ItemID   | Qty   |   isEquipped

1  |   1   |  1   |  0
1  |   2   |  0   |  0

for Items, I have

ItemID | ...... etc
1  | etc
2  |  etc

The results show ItemID 2 having a Qty of 1, which was making me pull my hair out last night.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: mysql question
« Reply #1 on: July 08, 2011, 08:41:17 AM »
Standard debug tips:
- don't use * but select fields manually
- put echo mysql_error(); after each query
- use capital letters for SQL reserved stuff, it will make your code more readable: "SELECT * FROM UserItems JOIN Items ON (UserItems.ItemID = Items.ItemID)"

Also you violated a database design rule (dunno how that rule was exactly called :D) UserItems does not have primary key (or you used ItemID as primary key in which case you messed it up totally since it is a foreign key). Try giving the person 2 identical swords and next try to delete 1 sword, you will see what I mean :)
« Last Edit: July 08, 2011, 10:05:37 AM by Chris »

Offline saljutin

  • Level 22
  • *
  • Posts: 266
  • Reputation: +6/-0
    • View Profile
Re: mysql question
« Reply #2 on: July 08, 2011, 09:00:58 AM »
mysql_query("YOUR QUERY") or die(mysql_error());
i think this is better then echo

so why dont use * but select fields manually...what if he wants to select everything?

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: mysql question
« Reply #3 on: July 08, 2011, 09:43:35 AM »
UserItem.Qty can be 0 to int

So... if I have 2 swords, I increase qty to 2, not add another row to the table.  I fail to see how this is a db design failure.

Also, I have an abstraction layer that adds the or die(mysql_error()) to the end.

This is just a query.  It is not returning the expected results.  Any help on what could be the problem, or are we critiquing my coding style? :D

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: mysql question
« Reply #4 on: July 08, 2011, 10:13:08 AM »
These were standard debug tips, not coding tips (I'm not insane enough to check mysql_error after each query or remove all * in production code) :) In case of trouble I follow these and it sometimes helps.

Quote
I believe Items might be a reserved keyword.
If you did mysql_error() already then why you ask about this? If this was a reserved keyword it would generate an error due to invalid syntax...

Quote
So... if I have 2 swords, I increase qty to 2, not add another row to the table.  I fail to see how this is a db design failure.
I know, some do not believe in the "unique ID for each row" rule, but I do (which is rare since I tend to break as many rules as possible :D). Anyway, I have a feeling you will pay for this later (like when you decide to make item enchantments), not that I wish you pay for this, not at all :)

Quote
Any help on what could be the problem, or are we critiquing my coding style?
a) well, if you have trouble with rather simple join it means it has something to do with coding style or some bigger mistake upon database schema design step
b) yes, critiquing someone's coding style is much more fun than providing a simple answer and you should know it by now :D

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: mysql question
« Reply #5 on: July 08, 2011, 10:58:14 AM »
Yeah, my system will get messy with enchantments.  I guess i need to drop Qty and add an InventoryID instead.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: mysql question
« Reply #6 on: July 08, 2011, 11:13:15 AM »
Don't drop it. Make only non equipment stackable. Works perfect (I did it that way in both Samurai and Moonstone).

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal