1

I am trying to execute a script from a server, the parameter is retrieved from MySQL query and stored into $test is there a way to use this as a parameter for my script? I have a script stored in a parameter as well $script

<?php

include("db.php");  
$user = 'user';
$password = 'pwd';

$script = '/my/path/to/script/script.sh';

...

$testResult = mysqli_query($dbConnection, $queryNextTest);
$test = $testResult->fetch_object()->test_script; //this is my parameter obtained from query

$stream = ssh2_exec($connection, $script); //how can I use test as my paramter to execute the 

?>

So what I am looking for is something like this: $stream = ssh2_exec($connection, '/my/path/to/script/script.sh $test'); but the $test cannot be inside the quotes.

tiger_groove
  • 956
  • 2
  • 17
  • 46

1 Answers1

2

You should change the quote to double and use curly bracket to express your variable like this:

$stream = ssh2_exec($connection, "{$script} {$test}");

Or even without curly braces like this:

$stream = ssh2_exec($connection, "$script $test");
Duc Filan
  • 6,769
  • 3
  • 21
  • 26
  • Am I allowed to combine two variables like this? `$stream = ssh2_exec($connection, "{$script} {$test}");` – tiger_groove Sep 16 '17 at 02:14
  • Yes, definitely. – Duc Filan Sep 16 '17 at 02:40
  • One more question, is it always good to use double quotes? What are the advantages from double quotes to single quotes and vice versa? – tiger_groove Sep 18 '17 at 21:28
  • 1
    No, you each one has its pros and cons in certain cases. You can refer this link for more info: https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – Duc Filan Sep 18 '17 at 22:42