PHP Variable Help

When I query my database I have a field which outputs the string $alang28.

It outputs in my browser as $alang28. I also have another file which I have included on the page which defines $alang = “username:”;

How can I make it not output the string but output whats in the variable?

have you used short PHP opening and closing tag? please check that and check your PHP configuration file, does it support short tags

it does support short tags

how you are browsing your PHP page or can you paste your code here

Ok so I have this code on my page:

$alang2 = “Hello there”;
PHP outputs this from a field in the MySQL database. Testing $alang2. It outputs on the webpage as:

Testing $alang2, where it should output: Testing Hello there

How can I make it output the variable, it outputs fine if it dosnt come from the database.

Paste the source code here, it is a bit hard to debug without the source. Sounds like you might not be using the database results correctly. try a var_dump or a print_r on the row results (object or array) from the database and make sure you are calling the correct variable.

<?php
$alang28 = "Hello there";

$query5 = mysql_query("SELECT * FROM onlineusers WHERE username ='$myuser' ")
or die("Could not insert data because ".mysql_error());
$qry5 = mysql_fetch_array( $query5 );

echo "Testing $qry5[message]";
echo "Testing without database $alang28";  // Works here
?>

qry5[message] outputs $alang28

That looks like it should work after the fetch array, try print_r($qry5); and post the output.

This is what it outputted:

Testing testing $alang28
again without database Hello thereArray ( [0] => 1 [id] => 1 [1] => chatpoint [username] => chatpoint [2] => blah [date] => blah [3] => testing $alang28 [command] => testing $alang28 ) daaa

I changed my code to this:

<?php
include "includes/config.php";
$alang28 = "Hello there";

$query5 = mysql_query("SELECT * FROM logs_staff WHERE id ='1' ")
or die("Could not insert data because ".mysql_error());
$qry5 = mysql_fetch_array( $query5 );

echo "Testing $qry5[command]<br />";
echo "again without database $alang28";  // Works here
print_r($qry5);
?> daaa

[command] => testing $alang28

Thats whats stored in the database, it is correctly working. Update the database if you want it to display something else. If you are trying to process the variable inside the output then you shouldnt be doing it this way but you can do it like this (bewarned this isnt great security)

eval(“print ‘Testing ‘.$qry5[command].’<br />’;”);

or something to that affect. I would however make that $alang28 come from the database, it would be a much better design.

hmmm I gave that code a go and I got internal server error

anyone else?

you can replace the variable with value using str_replace()

I don’t understand?

Single quote your keys and use brackets if echoing an array-key pair.

echo "Testing {$qry5['message']}";

I didn’t notice you were storing a variable in the database. Not a good idea. In any case, you might try this.

$var = preg_replace("/[^a-zA-Z0-9_]+/", "", $qry5['message']);
$qry5['message'] = $$var; 

echo "Testing {$qry5['message']}";

EDIT: Then again looking closer the variable is in a string so that’s not going to work.

I suppose this would work.


<?php
$words = explode(" ",$qry5['message']);

$sentence = array();
foreach($words as $str){
	if(strlen($str)>1 && $str[0] == "$"){
		$part = preg_replace("/[^a-zA-Z0-9_]+/", "", $str);
		$part = $$part;
	}else{
		$part = $str;
	}
	$sentence[] = $part;
}
$sentence = implode(" ",$sentence);
$qry5['message'] = $sentence;

echo "Testing {$qry5['message']}";
?>

awesome thanks!