SQL with variables in update argument

时间:2018-03-09 19:05:19

标签: php mysql sql

Is there a way to make a variable change the execute destination for an SQL command?

Basically, my question is, if it's possible through a PHP $variable to change the update destination for an SQL argument?

In this case, I have tried to update the column score by using a PHP $Variable.

How will I use this?, I will store the specific score destination "Destination, what row on the database" on the file that makes the update request, so I can avoid different config files "With config files, I mean different files for each game, example.

One game needs to save to Row1, I create a game config file that specifies where I will save my information to, then I have another game that will need to save to row2, I cant use the first game config file, therefore, a new config file needs to be made for that game.

Example on storage destination variable: $scoretype = "x" // Where X is the destination.

Why do I want to do this? I'm currently working on a game that has several different scores and therefore many different save destinations.

One destination and score for game1, one for game2 etc, this is a crucial part of the functionality of the website, hence my interesting question, to skip unnecessary server filling with different PHP config files for each game and for simplicity sake.

What I have been trying:

$id = $rid['id'];
$scoretype = "xpscore"; // Scoretype is where i will be saving the score in the database
$score = '1500'; // enabled for testing purposes
//$score = $_POST['uscore']; // disableded for testing purposes
$sql = "UPDATE users SET score='$score' WHERE id='$id' AND score='$scoretype'";  // I know there are two definitions for score here, just dont know how else i should declare where and what to save. 

Is this even possible to achieve?

I have not seen any tutorial or explanation explaining a scenario like this, therefore I'm curious if this actually could be achieved?

Sidenote I have a backup plan if this doesn't work or isn't achievable. Just wanted to see if there is a better alternative to making several "config.php" files just for changing the update destination which in this case will be where the score will be saved.

1 个答案:

答案 0 :(得分:-1)

你不应该为得分类型提供另一个领域吗?

UPDATE users SET score='$score' WHERE id='$id' AND scoretype='$scoretype'

或者将分数存储到另一个表中,其中包含用户ID,分数和分数类型的列。