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.

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
sanji
  • 121
  • 1
  • 14

3 Answers3

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.

Community
  • 1
  • 1
Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
  • 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. – Travis Weston Apr 23 '15 at 19:08
  • And with that edit, +1 for pretty much touching on every sane method of answering. – Travis Weston Apr 23 '15 at 19:10
0
$sql=("SELECT CONCAT('$path',`image`) FROM `testtable`");
Alex
  • 16,739
  • 1
  • 28
  • 51
0

Use concatenation like below....

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

Here, text in double quotes are string