2

i hawe some problem here. i want make a string to be a variable,

$query = mysql_query("SELECT * FROM users Where username = 'fahmi'");
$result = mysql_fetch_array($query);

Result of the array is - $result['data']="$a"

$a = "This is a Result 1" ;
$b = "This is a Result 2" ;
$c = "This is a Result 3" ;

$final = $result['data'];
echo $final;

When i ran this code, result is $a but i want the result is "This is a result 1", please help me, thanks...

6
  • 1
    looks like you might be wanting variable variables Commented Sep 21, 2016 at 4:39
  • 3
    Just remove the double Quotation from $a, like: $result['data'] = $a, $a is a variable so you not needs to use those ", Commented Sep 21, 2016 at 4:39
  • 1
    @FrayneKonok from how i understand it $a is stored in the database and the asker wants this data field to determine which variable to use Commented Sep 21, 2016 at 4:43
  • If the OP just store the a / b / c, then it will be just variable of variables. Ex: $final = $$result['data'];, and then echo $final. Commented Sep 21, 2016 at 4:47
  • 1
    Every-one above is correct, but in your case no mean, because $result actually have data from db in array format and you are over-writing this array with your $a,$b,$c variables. and from where they come from and what they mean here is not clear too. Commented Sep 21, 2016 at 4:47

1 Answer 1

1

If you want variable variables the you can achieve it this way:

$$a = "This is a Result 1" ;
$$b = "This is a Result 2" ;
$$c = "This is a Result 3" ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.