将变量从sqlplus传递到unix然后传递给php

时间:2011-10-19 08:45:35

标签: php shell unix sqlplus

我有一个调用shell脚本的php脚本。 shell脚本运行sql命令,输出传输到shell变量。 现在,如何将此变量传递给PHP脚本?

例如:PHP代码:

$_param1 = "JOHNDOE";
$out = exec("sh shellscript.ksh $_param1");
echo $out;

Shell脚本:

#!/bin/sh -u

param=$1;

out=`sqlplus -s $connectStr <<EOF

set serveroutput off;
set feedback off;
set heading off;
set pagesize 0;
set verify off;
set echo off;

select field from sometable where condition ='$param';
exit;
EOF`

echo $out;

我需要将$ out转移到PHP页面。除了创建文件之外还有其他方法吗?

1 个答案:

答案 0 :(得分:0)

将$ out作为'exec'的参数传递,以便shell的返回值存储在该变量上。

<强> myshell.sh

#!/bin/bash
echo $1

<强> myphp.php

<?php
$param = "HEY, this is Output";
exec("sh myshell.sh \"${param}\"", $out);
print_r($out);
?>

然后调用PHP脚本

$ php myphp.php 
Array
(
    [0] => HEY, this is Output
)

也许您在执行Shell时可能遇到一些权限问题,甚至您的PHP配置可能都是错误的。

相关问题