I have created a string, now I want to add data to the end of the string -

<p>This is the string</p>

I now want to read from MySql from a field and add the data to the end of the string. Something like -

&Str1 = "This is the first part";
&Str2 = My data from the database table field (Say for instance "Data"
<p>&Str1.$Str2</p> p will also not work in this context, how can I combine the 2 strings and then show it as say print?
6 Years
Discussion Span
Last Post by AndreRet

Again, this is more a question for the php and mysql experts, rather than site lay out and CSS, but let me try, as stated previously, it makes me learn these things as well, and hopefully an expert will correct any mistakes I make in trying to solve this one.

Maybe, the following will help?

$string = "your string" . $data;

The following manual may also be of great help if you are going to do a lot of php string things:


You will also find the print output string in there.


Thanks Kraai. I have asked the mods to move this to php. I have found a sample on the web which I'm busy trying out. Once done with that, I'm sure I'll have the answer.:)

I'll post it here. In the mean while I'll have a look at yours. For now, I'm off to home. My wife is here with me, jumping up and down already!!!;)


OK, first of all php variables begin with $ not &.


$first_string = "This is my string";
$result = mysql_query("SELECT field FROM table WHERE field_x = '...'");
$data = mysql_fetch_array($result);
$second_string = $data['field'];
$first_string .= " " . $second_string;

The field_x in the sql statement could be the same as field.
The last statement used the shorthand concatenator, which is equivalent to:

$first_string = $first_string . " " . $second_string;

You could do an 'all-in-one' statement like:

echo "<p>$first_string $second_string</p>";

Edited by diafol: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.