# Using PHP , call a sql inside a unix script

**URL:** <https://community.unix.com/t/using-php-call-a-sql-inside-a-unix-script/319120>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [October 31, 2012, 6:28am UTC](https://community.unix.com/t/using-php-call-a-sql-inside-a-unix-script/319120 "2012-10-31T06:28:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![madfox](https://community.unix.com/letter_avatar/madfox/32/5_5575768a8748004e209b776fc1b2916d.png) [@madfox](https://community.unix.com/u/madfox)\
**Post date:** [October 31, 2012, 6:28am UTC](https://community.unix.com/t/using-php-call-a-sql-inside-a-unix-script/319120/1 "2012-10-31T06:28:06Z")

</div>

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

```plaintext
#!/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](http://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]

---

<div class="post-metadata">

**Author:** ![eosbuddy](https://community.unix.com/letter_avatar/eosbuddy/32/5_5575768a8748004e209b776fc1b2916d.png) [@eosbuddy](https://community.unix.com/u/eosbuddy)\
**Post date:** [October 31, 2012, 6:49am UTC](https://community.unix.com/t/using-php-call-a-sql-inside-a-unix-script/319120/2 "2012-10-31T06:49:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![madfox](https://community.unix.com/letter_avatar/madfox/32/5_5575768a8748004e209b776fc1b2916d.png) [@madfox](https://community.unix.com/u/madfox)\
**Post date:** [October 31, 2012, 7:21am UTC](https://community.unix.com/t/using-php-call-a-sql-inside-a-unix-script/319120/3 "2012-10-31T07:21:01Z")

</div>

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

```nohighlight
#!/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

```
