Click to See Complete Forum and Search --> : Selecting from database using a variable name


tobyw_1969
11-24-2003, 08:56 AM
I am trying to use PHP to keep the results of a quiz in a database. I want to be able to record the total number of players, the total score, and the distribution of the scores from 1-20.

I want to do this with one entry in a database with columns for each of the possible scores like this

total_players, total_score, 1, 2, 3, 4, 5, 6, 7, 8, ..etc


Then I am trying to update the relevant column each time the quiz is completed.

The problem I am having is trying to access the columns 1-20. I don't know how to use a variable to retrieve and set the contents of a column. So far I am doing this



$score = $_REQUEST["score"];

$query = "select * from naaas where country = '".$country."'";
$result = mysql_query($query);
$row=mysql_fetch_array($result);

$oldtotal = stripslashes($row['total_players']);
$oldtotalscore = stripslashes($row['total_score']);
$oldspecificscore = stripslashes($row['.$score']"



This correctly retrieves the current score and total players, but I don't know how to do the 3rd one. For example, can I strip out the column labelled '2' using a variable with a value of '2' - and if so what should be the syntax?

Many thanks for any help you can give.

Toby

pyro
11-24-2003, 09:08 AM
Try:

$oldspecificscore = stripslashes($row[$score]);

tobyw_1969
11-24-2003, 10:16 AM
Thanks pyro - actually I had a stray . in there which I mistyped. I still had problems with the [$score] thing. but I tried everything and this actually worked

$oldspecificscore = stripslashes($row["$score"]);

not very obvious...but I'm guessing it was confused about whether $score should be a string or a number. Anyway, thanks for the quick reply.

pyro
11-24-2003, 11:06 AM
Yes, double quotes would work as well. With double quotes, PHP has variable interpolation, whereas with single quotes, it does not...