0

I have a problem in add value in SELECT query.

$sql=("SELECT `image` FROM `testtable`");

The output: 123.jpg

But I want output: 127.0.0.1/home/galery/123.jpg

So I tried:

$path='127.0.0.1/home/galery/';
.........

$sql=("SELECT $path+`image` FROM `testtable`");

But it's not working.

2
  • have you saved this value in your database..?? Commented Apr 23, 2015 at 18:39
  • if not then you should concatenate your output value with path .. Commented Apr 23, 2015 at 18:40

3 Answers 3

1

There are two ways to accomplish this.

Method 1:

Use string concatenation to join the path to the result from the SQL:

$path = '127.0.0.1/home/galery/';
$sql = "SELECT `image` FROM `testtable`";

// Run the query...

$result = $path . $sql;

In php, string concatenation is performed with the . operator. Also see here.

Method 2:

The second method is via the CONCAT SQL function:

$sql = "SELECT CONCAT('" . $path . "', `image`) FROM `testtable`";

Or:

$sql = "SELECT CONCAT('{$path}', `image`) FROM `testtable`";

See this question for the difference between these options.

Sign up to request clarification or add additional context in comments.

2 Comments

Your first example in Method 2 is incorrect, as wrapping $path in single quotes will make it parse as the literal $path, so you'd have to remove the single quotes or use the second example, which doesn't actually need the curly braces around $path, but those are useful for readability.
And with that edit, +1 for pretty much touching on every sane method of answering.
0
$sql=("SELECT CONCAT('$path',`image`) FROM `testtable`");

Comments

0

Use concatenation like below....

$sql=("SELECT".$path."+image FROM test")

Here, text in double quotes are string

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.