Using PHP , call a sql inside a unix script

I am running the xampp on WINDOWS, and my php script is connecting to a unix script on a different server (ssh2_connect("11.31.138.56", 22). I am running the unix script and inside this script I am calling the .sql file . The SQL is connecting to oracle db on the unix server.

But the sqlplus command is not working when called using php , but when I run the script on unix everything is working fine.

ERROR : Output: Error: mysql2.sh: line 8: sqlplus: command not found

Unix script:mysql2.sh

#!/bin/ksh

sqlplus -s XLTDB10/XLTDB10@XLTUAT1 @mysql.sql 

PHP CODE:

<?php
include 'db_connect.php';
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect("11.31.138.56", 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password
if(!ssh2_auth_password($con, "uat0", "unix11")) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";

    // execute a command
    if \(!\($stream = ssh2_exec\($con,"sh mysql2.sh" \)\)\) \{
        echo "fail: unable to execute command\\n";
    \} else \{
        // collect returning data from command

        $errorStream = ssh2\_fetch_stream\($stream, SSH2\_STREAM_STDERR\);

        // Enable blocking for both streams

stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
// Whichever of the two below commands is listed first will receive its appropriate output. The second command receives nothing
echo "Output: " . stream_get_contents($stream);
echo "Error: " . stream_get_contents($errorStream);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
// echo $data;
}
// echo $stream;
fclose($errorStream);
fclose($stream);
}
}
}
?>[/code]

You need to set the full path to sqlplus in the script.

1 Like

this below solution worked for me,what I did , I changed the unix script like this.

#!/bin/ksh

export ORACLE_HOME=/oravl01/oracle/11.2.0.3
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=xltuat1

/oravl01/oracle/11.2.0.3/bin/sqlplus -s XLTDB10/XLTDB10@XLTUAT1 @mysql.sql