• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

mysql/php

DeathByTray

New Member
Joined
Dec 21, 2009
Messages
63 (0.01/day)
System Name Dustbuster
Processor AMD Phenom II X4 "Deneb C3" 955 Black, 4x 3.2GHz
Motherboard GIGABYTE GA-770TA-UD3
Cooling COOLERMASTER Hyper 212 Plus
Memory Kingston Technology DDR3 4GB 1333MHZ
Video Card(s) SAPPHIRE HD 5850 1GB GDDR5
Storage WESTERN DIGITAL Caviar Black 2.0TB
Display(s) SAMSUNG SyncMaster P2450 24" TFT LCD
Case BITFENIX Survivor Core
Audio Device(s) OnBoard
Power Supply ARCTIC COOLING Fusion 550R
Software 7
I got stuck and can't figure out why.

PHP:
$gpu = "a5970,a5870,a5850,a5830,a5770,a5750,a5670,a480,a470,";
$gpuprize = "'699','389','289','229','134','119','79','499','349',";
$stuffit = "INSERT INTO	price (".$gpu." date) VALUES ('".$gpuprize."'CURDATE())";
mysql_query($stuffit) or die(mysql_error());
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '699','389','289','229','134','119','79','499','349','CURDATE())' at line 1

Any idea?
I first thought it was due to missing backticks in $gpuprize, added them and still not working.
 
Joined
May 20, 2004
Messages
10,487 (1.44/day)
You're missing commas between the column names and the values. Also, you can't just insert an endless amount of values. One value per colum, ie run a query for each value.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
You also shouldn't have to put apostrophes around numbers:
Code:
$gpuprize = "699, 389, 289, 229, 134, 119, 79, 499, 349,";
$stuffit = "INSERT INTO price (" . $gpu . " date) VALUES (" . $gpuprize . " CURDATE())";

That's assuming a5970, a5870, etc. are column names in table "price."
 

DeathByTray

New Member
Joined
Dec 21, 2009
Messages
63 (0.01/day)
System Name Dustbuster
Processor AMD Phenom II X4 "Deneb C3" 955 Black, 4x 3.2GHz
Motherboard GIGABYTE GA-770TA-UD3
Cooling COOLERMASTER Hyper 212 Plus
Memory Kingston Technology DDR3 4GB 1333MHZ
Video Card(s) SAPPHIRE HD 5850 1GB GDDR5
Storage WESTERN DIGITAL Caviar Black 2.0TB
Display(s) SAMSUNG SyncMaster P2450 24" TFT LCD
Case BITFENIX Survivor Core
Audio Device(s) OnBoard
Power Supply ARCTIC COOLING Fusion 550R
Software 7
Ahh.. thanks a lot FordGT90Concept, works like a charm.
Those damn apostrophes in VALUES were not needed. Weird, I thought they were necessary.

DanTheBanjoman
Commas as well as enough columns are there, I was just under the impression that you need apostrophes as well as quotation marks when you're using variables in VALUES.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I found that MySQL often prefers ` (to the left of 1 on QWERTY keyboards) to '. If you replaced all the apostrophes with tlides (`), it probably would have worked. You only need to put ` around strings.
 

DeathByTray

New Member
Joined
Dec 21, 2009
Messages
63 (0.01/day)
System Name Dustbuster
Processor AMD Phenom II X4 "Deneb C3" 955 Black, 4x 3.2GHz
Motherboard GIGABYTE GA-770TA-UD3
Cooling COOLERMASTER Hyper 212 Plus
Memory Kingston Technology DDR3 4GB 1333MHZ
Video Card(s) SAPPHIRE HD 5850 1GB GDDR5
Storage WESTERN DIGITAL Caviar Black 2.0TB
Display(s) SAMSUNG SyncMaster P2450 24" TFT LCD
Case BITFENIX Survivor Core
Audio Device(s) OnBoard
Power Supply ARCTIC COOLING Fusion 550R
Software 7
Interesting, will keep an eye on that.

While we're at it, I've got another question.
As you can see, I'm using a prefix - 'a' in this case - for all the GPU names. That's because mysql doesn't seem to like numbers only. Do you know of a better workaround or a fix?
 
Last edited:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
No offense but you are doing it wrong. XD

There should be a table containing a list of graphics cards. Price should either be part of that table or in a separate table. For example...

GfxCards Table:
id|model|vram
1|"5970"|2048
2|"5870"|1024

GfxCardPrice Table:
id|gfx_id|price|date
1|1|699|NOW()
2|2|389|NOW()


On that second table, if you want to log a new price on 5870, for example, it would appear as:
id|gfx_id|price|date
1|1|699|NOW()
2|2|389|NOW()
3|2|350|NOW()

If you were looking for the price of a 5870, you would query all WHERE gfx_id = 2 and take the most recent entry.
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
27,046 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
in my opinion it is good practice to put field values into ' even if they are numbers

also dont forget to clean user input (includes $_POST and $_GET), look at mysql_escape_string

i use the backtick ` only when escaping field names, for example when you call your field "date" .. mysql thinks you want to call some date function, use `date` to identify it as a field name, better is to call it ts like timestamp or dateadded or something that is not a mysql keyword, will save you headaches later
 

DeathByTray

New Member
Joined
Dec 21, 2009
Messages
63 (0.01/day)
System Name Dustbuster
Processor AMD Phenom II X4 "Deneb C3" 955 Black, 4x 3.2GHz
Motherboard GIGABYTE GA-770TA-UD3
Cooling COOLERMASTER Hyper 212 Plus
Memory Kingston Technology DDR3 4GB 1333MHZ
Video Card(s) SAPPHIRE HD 5850 1GB GDDR5
Storage WESTERN DIGITAL Caviar Black 2.0TB
Display(s) SAMSUNG SyncMaster P2450 24" TFT LCD
Case BITFENIX Survivor Core
Audio Device(s) OnBoard
Power Supply ARCTIC COOLING Fusion 550R
Software 7
FordGT90Concept
Thanks but why is that any better than my table? I just can't see the advantage.

Price Table:
id|a5850|a5870|date
1|400|300|NOW()
2|405|305|NOW()

Card table
id|model|vram
1|5850|1024
2|5870|1024

My layout gives me more columns while your layout gives me more rows. Excuse my ignorance buy why is the latter better?

W1zzard
Good to know, will change it asap.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
For example, you want to find all cards worth $300, instead of searching one table like:

Code:
SELECT price, gfx_id FROM GfxCardPrice WHERE price = 300

...you would have to do...

Code:
SELECT a5850, a5870 FROM price where a5850 = 300 OR a5870 = 300

For every additional column in the table organized in that way, your query gets longer and longer (slower and slower). The query would also have to be updated every time a new column is added. Where as the "right" way of doing it, the same query would always work so long as `price` and `gfx_id` exists.

Moreover, how will you handle blanks when there is no price change at this time on a specific card? It would probably be the same, wouldn't it? For example:
id|a5850|a5870|date
1|400|300|NOW()
2|405|305|NOW()
3|425|305|NOW()
The two 305s should be normalized but because of the way the table is laid out, there's no way to normalize it meaning the table will always have wasted space.
 
Last edited:
Joined
May 20, 2004
Messages
10,487 (1.44/day)
in my opinion it is good practice to put field values into ' even if they are numbers

Didn't we have a discussion about this some weeks ago? Integer between quotes > ODBC no likey. Obviously this doesn't apply here but it was a fair example of why it is actually wrong.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Putting " ' ` around numbers treats it as a string rather than a numeric value. Some interpreters don't care, some do, as Dan pointed out.
 

DeathByTray

New Member
Joined
Dec 21, 2009
Messages
63 (0.01/day)
System Name Dustbuster
Processor AMD Phenom II X4 "Deneb C3" 955 Black, 4x 3.2GHz
Motherboard GIGABYTE GA-770TA-UD3
Cooling COOLERMASTER Hyper 212 Plus
Memory Kingston Technology DDR3 4GB 1333MHZ
Video Card(s) SAPPHIRE HD 5850 1GB GDDR5
Storage WESTERN DIGITAL Caviar Black 2.0TB
Display(s) SAMSUNG SyncMaster P2450 24" TFT LCD
Case BITFENIX Survivor Core
Audio Device(s) OnBoard
Power Supply ARCTIC COOLING Fusion 550R
Software 7
Thanks for the explanation but I'm still missing the aha experience, I just can't get it into my head. Anyway, I will take your advice and maybe I'll get it along the way.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I'll expand my previous example with all the columns you inserted data into previously:
Code:
SELECT a5970, a5870, a5850, a5830, a5770, a5750, a5670, a480, a470 FROM price WHERE a5970 = 300 OR a5870 = 300 OR a5850 = 300 OR a5830 = 300 OR a5770 = 300 OR a5750 = 300 OR a5670 = 300 OR a480 = 300 OR a470 = 300
...compared to...
Code:
SELECT price, gfx_id FROM GfxCardPrice WHERE price = 300

If you had to had 6870 later, for example, you'd have to modify your query to:
Code:
SELECT a6870, a5970, a5870, a5850, a5830, a5770, a5750, a5670, a480, a470 FROM price WHERE a6870 = 300, a5970 = 300 OR a5870 = 300 OR a5850 = 300 OR a5830 = 300 OR a5770 = 300 OR a5750 = 300 OR a5670 = 300 OR a480 = 300 OR a470 = 300
...when again, the same code above would still work...
Code:
SELECT price, gfx_id FROM GfxCardPrice WHERE price = 300

That should make it eaiser to see how much work you are saving yourself down the road.


If you had to change it from 300 to something else, you only have to edit it one value versus 9+ values.
 

DeathByTray

New Member
Joined
Dec 21, 2009
Messages
63 (0.01/day)
System Name Dustbuster
Processor AMD Phenom II X4 "Deneb C3" 955 Black, 4x 3.2GHz
Motherboard GIGABYTE GA-770TA-UD3
Cooling COOLERMASTER Hyper 212 Plus
Memory Kingston Technology DDR3 4GB 1333MHZ
Video Card(s) SAPPHIRE HD 5850 1GB GDDR5
Storage WESTERN DIGITAL Caviar Black 2.0TB
Display(s) SAMSUNG SyncMaster P2450 24" TFT LCD
Case BITFENIX Survivor Core
Audio Device(s) OnBoard
Power Supply ARCTIC COOLING Fusion 550R
Software 7
Mhh.. yes, not sure if I would ever use it that way though. I'd much rather get all prices from a specific day but if I'm not mistaken, I'd still have to select all the different cards.

If I take your example, I'd get lots of prices from different dates. I guess you could further specify the exact date, so it would be alright.
PHP:
SELECT price, gfx_id FROM GfxCardPrice WHERE price = 300


I'm seeing the light, I think. :p
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
27,046 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
GROUP BY gfx_id

look it up in the mysql docs

or SORT BY `date` DESC LIMIT 1 to get the most recent one
 

DeathByTray

New Member
Joined
Dec 21, 2009
Messages
63 (0.01/day)
System Name Dustbuster
Processor AMD Phenom II X4 "Deneb C3" 955 Black, 4x 3.2GHz
Motherboard GIGABYTE GA-770TA-UD3
Cooling COOLERMASTER Hyper 212 Plus
Memory Kingston Technology DDR3 4GB 1333MHZ
Video Card(s) SAPPHIRE HD 5850 1GB GDDR5
Storage WESTERN DIGITAL Caviar Black 2.0TB
Display(s) SAMSUNG SyncMaster P2450 24" TFT LCD
Case BITFENIX Survivor Core
Audio Device(s) OnBoard
Power Supply ARCTIC COOLING Fusion 550R
Software 7
Roger, redoing my database then.
 

DeathByTray

New Member
Joined
Dec 21, 2009
Messages
63 (0.01/day)
System Name Dustbuster
Processor AMD Phenom II X4 "Deneb C3" 955 Black, 4x 3.2GHz
Motherboard GIGABYTE GA-770TA-UD3
Cooling COOLERMASTER Hyper 212 Plus
Memory Kingston Technology DDR3 4GB 1333MHZ
Video Card(s) SAPPHIRE HD 5850 1GB GDDR5
Storage WESTERN DIGITAL Caviar Black 2.0TB
Display(s) SAMSUNG SyncMaster P2450 24" TFT LCD
Case BITFENIX Survivor Core
Audio Device(s) OnBoard
Power Supply ARCTIC COOLING Fusion 550R
Software 7
It's me again. Haven't had much time but I just finished implementing all the changes. Everything works perfectly, there are just two cosmetic things that bother me.

1)
PHP:
while ($e <= $g)
	{
	$stuffit = "INSERT INTO	price (gfxid,price,dateadd) VALUES (".$gfxid[$e].",".$gpuprize[$e].",CURDATE())";
	mysql_query($stuffit) or die(mysql_error());
	$e++;
	}
As you see, I need a loop for every query and I'm not sure how much that hampers the performance or if it's negligible. Is it possible to put everything into one query? I guess not :p


2) How do I reset the ID count apart from readding the column?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
1) It shouldn't be a problem unless $g is really high. The PHP server will kill any script that takes longer than X number of seconds to execute. If it works, it should be fine but that isn't a query I'd run very frequently because it will obviously make the database bigger.

2) I don't think there is anyway to change the index number on a column marked as a primary index. Every entry is unique and reseting the number would damage the integrity of the database. It won't hurt anything to leave it like that.
 

DeathByTray

New Member
Joined
Dec 21, 2009
Messages
63 (0.01/day)
System Name Dustbuster
Processor AMD Phenom II X4 "Deneb C3" 955 Black, 4x 3.2GHz
Motherboard GIGABYTE GA-770TA-UD3
Cooling COOLERMASTER Hyper 212 Plus
Memory Kingston Technology DDR3 4GB 1333MHZ
Video Card(s) SAPPHIRE HD 5850 1GB GDDR5
Storage WESTERN DIGITAL Caviar Black 2.0TB
Display(s) SAMSUNG SyncMaster P2450 24" TFT LCD
Case BITFENIX Survivor Core
Audio Device(s) OnBoard
Power Supply ARCTIC COOLING Fusion 550R
Software 7
Alright, will come back when I run into my next problem!
 
Top