0

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jonas
  • 85
  • 11
  • I do not understand what you mean *destination* especially on related to config files. And why is *score* in `SET` and `WHERE` clause? I will say this: you can use `IF` and `CASE` conditional logic in `WHERE` clauses. – Parfait Mar 09 '18 at 19:12
  • Thank you for the feedback, I will update my question to further explain what I want to achieve. – Jonas Mar 09 '18 at 19:22
  • If your question is whether you could use variable column names as well as values, the answer is *Yes*, a query is just a string and you can parse it with whatever variable-value substitutions before executing it. – Majid Fouladpour Mar 09 '18 at 20:19

1 Answers1

-1

Shouldn't you be having another field for scoretype?

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

Or store scores to another table where you have columns for user id, score and score type.

Sisuman
  • 29
  • 5
  • Questions should be asked in comments rather than posted as answers. – Eric Brandt Mar 09 '18 at 19:45
  • Doesn't that mean I have to create another table with just scores? As it is now I have id name email score xpscore . SET score='$score' will just update my score but not xpscore, i just want to update one, therefore" AND scoretype='$scoretype' should not make a difference – Jonas Mar 09 '18 at 19:55
  • Question went mistakenly to answer, sorry. – Sisuman Mar 09 '18 at 20:08