PHP致命错误:对非对象调用成员函数Execute()

PHP致命错误:对非对象调用成员函数Execute(),php,Php,当我执行这段代码时,我得到一个错误,上面写着“PHP致命错误:对非对象调用成员函数execute()。导致此错误的行是第96行:$ok=$DB->Execute($sql,$aWhereParams);我试图解决这个问题,但没有成功。可能代码没有返回对象?我如何捕获此错误,或者我的代码是否有问题?请帮忙。谢谢 西克维特 <?php include_once 'config.php'; include_once('/apps/geoservices/apps/geoservices2.4

当我执行这段代码时,我得到一个错误,上面写着“PHP致命错误:对非对象调用成员函数execute()。导致此错误的行是第96行:$ok=$DB->Execute($sql,$aWhereParams);我试图解决这个问题,但没有成功。可能代码没有返回对象?我如何捕获此错误,或者我的代码是否有问题?请帮忙。谢谢

西克维特

<?php

include_once 'config.php';
include_once('/apps/geoservices/apps/geoservices2.4/config/settings.php');
include_once('/apps/geoservices/apps/geoservices2.4/htdocs/utils.php');

if ($argc < 2)
{
    echo "Usage: php daemon.php <samba share>\n";
    exit(1);
}
elseif (is_dir($argv[1]))
{
    global $base;
    $base = $argv[1];

    // Dir_walk recursively walks to the root directory and all the subdirectory's
    dir_walk('handleFile', $argv[1], array('xml'), true, '', $argv[1]);
}
else
{
    echo "The parameter given for the samba share is not a directory.\n";
    exit(1);
}

function handleFile($base, $dir, $filehandle)
{
    //Get the application name
    $application_name = substr($dir, 0, -1);
    $application_name = strtolower(str_replace('\\', '_', $application_name)."_".substr($filehandle, 0, strrpos($filehandle, '.')));


    // Set the parameters needed to insert the WMC into the database
    $aWhereParams = array();

    $file = $base.$dir.$filehandle;
    $aWhereParams['appname'] = strtolower($application_name);
    $aWhereParams['password'] = generatePassword();
    $aWhereParams['wmctitle'] = null;
    $aWhereParams['folder'] = null;
    $aWhereParams['startwmc'] = 'Y';
    $aWhereParams['userid'] = null;

    $DB = createDatabaseConnection($dbconnect, $dbuser, $dbpasswd);

    if ($aWhereParams['folder'] !== null) 
    {
        // check if folder is already created
        $sql = "SELECT FOLDERID FROM WMCFOLDERS WHERE FOLDERTITLE = :folder AND APPLICATION=:appname";
        $ok = $DB->Execute($sql, $aWhereParams);
        if ($ok) 
        {
            $myArray = $ok->GetArray();
            if (empty($myArray)) 
            {
                // insert the folder into WMCFOLDERS
                createWMCDirectory($DB, $aWhereParams);
            } 
            else 
            {
                foreach ($myArray as $row) 
                {
                    $aWhereParams['folderid'] = $row['FOLDERID'];
                }
            }
        }
    } 
    else 
    {
        $aWhereParams['folderid'] = null;
    }

    $folderIdValue;
    if ($aWhereParams['folderid'] === null) 
    {
        $folderIdValue = 'IS NULL';
    } 
    else 
    {
        $folderIdValue = '='.$aWhereParams['folderid'];
    }

    $titleValue;
    if ($aWhereParams['wmctitle'] === null) 
    {
        $titleValue = 'IS NULL';
    } 
    else 
    {
        $titleValue = '='.$DB->qstr($aWhereParams['wmctitle'], get_magic_quotes_gpc());
    }

    // check if there is an existing entry, if so update it
    $sql = "SELECT APPLICATION FROM WMC WHERE STARTWMC=:startwmc AND APPLICATION=:appname AND FOLDERID ".$folderIdValue." AND TITLE ".$titleValue;
    $ok = $DB->Execute($sql, $aWhereParams);
    if ($ok) 
    {
        $myArray = $ok->GetArray();
        if (empty($myArray)) 
        {   
            // we need to insert a new entry
            $sql = "insert into WMC (USERID, APPLICATION, FOLDERID, TITLE, STARTWMC, WMC) values (null, :appname, :folderid, :wmctitle, :startwmc, null)";

            // Combine WMC name with the password for the new entry
            $aWhereParams['appname'] .= $aWhereParams['password'];

            $ok = $DB->Execute($sql, $aWhereParams);
            if (!$ok) 
            {
                handleDBError($DB);
            }

            insertCLOB($DB, $file, $aWhereParams['appname'], $aWhereParams['folderid'] , $aWhereParams['wmctitle']);

            foreach ($GLOBALS['emailadresses'] as $dienst => $emailaddress)
            {
                if ($dienst == $aWhereParams['appname'])
                {
                    // Set the email properties
                    $to = $emailaddress;
                    $subject = "Nieuwe WMC toegevoegd";
                    $message = "Er is een nieuwe WMC toegevoegd. Je kunt deze bekijken door naar het adres ".getURL()." te gaan en in te loggen met \n";
                    $message .= "gebruikersnaam: ".$aWhereParams['appname'];
                    $message .= "wachtwoord: ".$aWhereParams['password'];

                    $from = "test@blabla.com";
                    $headers = "From:".$from;

                    // Combine all property's and send the email
                    mail($to, $subject, $message, $headers);
                }
            }
        } 
        else 
        {
            insertCLOB($DB, $file, $aWhereParams['appname'], $aWhereParams['folderid'] , $aWhereParams['wmctitle']);

            foreach ($GLOBALS['emailadresses'] as $dienst => $emailaddress)
            {
                if ($dienst == $aWhereParams['appname'])
                {
                    // Set the email properties
                    $to = $emailaddress;
                    $subject = "Nieuwe WMC toegevoegd";
                    $message = "Er is een nieuwe WMC toegevoegd. Je kunt deze bekijken door naar het adres ".getURL()." te gaan.";

                    $from = "test@blabla.com";
                    $headers = "From:".$from;

                    // Combine all property's and send the email
                    mail($to, $subject, $message, $headers);
                }
            }
        }
    } 
    else 
    {
        handleDBError($DB);
    }

    echo "WMC '".$aWhereParams['wmctitle']."' loaded OK in folder '".$aWhereParams['folder']."'\n";
    echo "    for ".$aWhereParams['appname'].", $dbuser@$dbconnect\n";
}

// Generate a random password
function generatePassword()
{
    $length = 8;

    $password = "";
    $possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";

    $maxlength = strlen($possible);

    if ($length > $maxlength)
        $length = $maxlength;

    $i = 0; 
    while ($i < $length) 
    { 
      $char = substr($possible, mt_rand(0, $maxlength-1), 1);

      if (!strstr($password, $char)) 
      { 
        $password .= $char;
        $i++;
      }
    }

    return $password;
}

function getURL()
{
    //This will be URL
    return gethostname()."/apps/geoservices/";
}

function dir_walk($callback, $dir, $types = null, $recursive = false, $baseDir = '', $base)
{
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false)
        {
            if ($file === '.' || $file === '..')
            {
                continue;
            }
            if (is_file($dir . $file))
            {
                if (is_array($types))
                {
                    if (!in_array(strtolower(pathinfo($dir . $file, PATHINFO_EXTENSION)), $types, true))
                    {
                        continue;
                    }
                }
                $callback($base, $baseDir, $file);
            }
            elseif($recursive && is_dir($dir . $file))
            {
                dir_walk($callback, $dir . $file . DIRECTORY_SEPARATOR, $types, $recursive, $baseDir . $file . DIRECTORY_SEPARATOR, $base);
            }
        }
        closedir($dh);
    }
}
?>
$emailaddress)
{
如果($dienst==$aWhereParams['appname']))
{
//设置电子邮件属性
$to=$emailaddress;
$subject=“Nieuwe WMC toegevoegd”;
$message=“Er是一个新的WMC toegevoegd.Je kunt deze bekijken door naar het adres.”getURL()“te gaan.”;
$from=”test@blabla.com";
$headers=“From:”.$From;
//合并所有属性并发送电子邮件
邮件($to、$subject、$message、$headers);
}
}
}
} 
其他的
{
handleDBError($DB);
}
回显“WMC”。$aWhereParams['wmctitle']。“'loaded OK in folder'”。$aWhereParams['folder']。“\n”;
echo“对于“$aWhereParams['appname']”,$dbuser@$dbconnect\n”;
}
//生成随机密码
函数generatePassword()
{
$length=8;
$password=“”;
$mablue=“2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ”;
$maxlength=strlen($maxlength);
如果($length>$maxlength)
$length=$maxlength;
$i=0;
而($i<$length)
{ 
$char=substr($maxblue,mt_rand(0,$maxlength-1),1);
如果(!strstr($password,$char))
{ 
$password.=$char;
$i++;
}
}
返回$password;
}
函数getURL()
{
//这将是一个URL
返回gethostname()。“/apps/geoservices/”;
}
函数dir\u walk($callback,$dir,$types=null,$recursive=false,$baseDir='',$base)
{
如果($dh=opendir($dir)){
while(($file=readdir($dh))!==false)
{
如果($file=='.| |$file==''.')
{
继续;
}
if(is_文件($dir.$file))
{
if(is_数组($types))
{
if(!in_数组(strtolower(路径信息($dir.$file,路径信息_扩展名)),$types,true))
{
继续;
}
}
$callback($base,$baseDir,$file);
}
elseif($recursive&&is_dir($dir.$file))
{
dir_walk($callback,$dir.$file.DIRECTORY_SEPARATOR,$types,$recursive,$baseDir.$file.DIRECTORY_SEPARATOR,$base);
}
}
closedir($dh);
}
}
?>

<?php

include_once 'config.php';
include_once('/apps/geoservices/apps/geoservices2.4/config/settings.php');
include_once('/apps/geoservices/apps/geoservices2.4/htdocs/utils.php');

if ($argc < 2)
{
    echo "Usage: php daemon.php <samba share>\n";
    exit(1);
}
elseif (is_dir($argv[1]))
{
    global $base;
    $base = $argv[1];

    // Dir_walk recursively walks to the root directory and all the subdirectory's
    dir_walk('handleFile', $argv[1], array('xml'), true, '', $argv[1]);
}
else
{
    echo "The parameter given for the samba share is not a directory.\n";
    exit(1);
}

function handleFile($base, $dir, $filehandle)
{
    //Get the application name
    $application_name = substr($dir, 0, -1);
    $application_name = strtolower(str_replace('\\', '_', $application_name)."_".substr($filehandle, 0, strrpos($filehandle, '.')));


    // Set the parameters needed to insert the WMC into the database
    $aWhereParams = array();

    $file = $base.$dir.$filehandle;
    $aWhereParams['appname'] = strtolower($application_name);
    $aWhereParams['password'] = generatePassword();
    $aWhereParams['wmctitle'] = null;
    $aWhereParams['folder'] = null;
    $aWhereParams['startwmc'] = 'Y';
    $aWhereParams['userid'] = null;

    $DB = createDatabaseConnection($dbconnect, $dbuser, $dbpasswd);

    if ($aWhereParams['folder'] !== null) 
    {
        // check if folder is already created
        $sql = "SELECT FOLDERID FROM WMCFOLDERS WHERE FOLDERTITLE = :folder AND APPLICATION=:appname";
        $ok = $DB->Execute($sql, $aWhereParams);
        if ($ok) 
        {
            $myArray = $ok->GetArray();
            if (empty($myArray)) 
            {
                // insert the folder into WMCFOLDERS
                createWMCDirectory($DB, $aWhereParams);
            } 
            else 
            {
                foreach ($myArray as $row) 
                {
                    $aWhereParams['folderid'] = $row['FOLDERID'];
                }
            }
        }
    } 
    else 
    {
        $aWhereParams['folderid'] = null;
    }

    $folderIdValue;
    if ($aWhereParams['folderid'] === null) 
    {
        $folderIdValue = 'IS NULL';
    } 
    else 
    {
        $folderIdValue = '='.$aWhereParams['folderid'];
    }

    $titleValue;
    if ($aWhereParams['wmctitle'] === null) 
    {
        $titleValue = 'IS NULL';
    } 
    else 
    {
        $titleValue = '='.$DB->qstr($aWhereParams['wmctitle'], get_magic_quotes_gpc());
    }

    // check if there is an existing entry, if so update it
    $sql = "SELECT APPLICATION FROM WMC WHERE STARTWMC=:startwmc AND APPLICATION=:appname AND FOLDERID ".$folderIdValue." AND TITLE ".$titleValue;
    $ok = $DB->Execute($sql, $aWhereParams);
    if ($ok) 
    {
        $myArray = $ok->GetArray();
        if (empty($myArray)) 
        {   
            // we need to insert a new entry
            $sql = "insert into WMC (USERID, APPLICATION, FOLDERID, TITLE, STARTWMC, WMC) values (null, :appname, :folderid, :wmctitle, :startwmc, null)";

            // Combine WMC name with the password for the new entry
            $aWhereParams['appname'] .= $aWhereParams['password'];

            $ok = $DB->Execute($sql, $aWhereParams);
            if (!$ok) 
            {
                handleDBError($DB);
            }

            insertCLOB($DB, $file, $aWhereParams['appname'], $aWhereParams['folderid'] , $aWhereParams['wmctitle']);

            foreach ($GLOBALS['emailadresses'] as $dienst => $emailaddress)
            {
                if ($dienst == $aWhereParams['appname'])
                {
                    // Set the email properties
                    $to = $emailaddress;
                    $subject = "Nieuwe WMC toegevoegd";
                    $message = "Er is een nieuwe WMC toegevoegd. Je kunt deze bekijken door naar het adres ".getURL()." te gaan en in te loggen met \n";
                    $message .= "gebruikersnaam: ".$aWhereParams['appname'];
                    $message .= "wachtwoord: ".$aWhereParams['password'];

                    $from = "test@blabla.com";
                    $headers = "From:".$from;

                    // Combine all property's and send the email
                    mail($to, $subject, $message, $headers);
                }
            }
        } 
        else 
        {
            insertCLOB($DB, $file, $aWhereParams['appname'], $aWhereParams['folderid'] , $aWhereParams['wmctitle']);

            foreach ($GLOBALS['emailadresses'] as $dienst => $emailaddress)
            {
                if ($dienst == $aWhereParams['appname'])
                {
                    // Set the email properties
                    $to = $emailaddress;
                    $subject = "Nieuwe WMC toegevoegd";
                    $message = "Er is een nieuwe WMC toegevoegd. Je kunt deze bekijken door naar het adres ".getURL()." te gaan.";

                    $from = "test@blabla.com";
                    $headers = "From:".$from;

                    // Combine all property's and send the email
                    mail($to, $subject, $message, $headers);
                }
            }
        }
    } 
    else 
    {
        handleDBError($DB);
    }

    echo "WMC '".$aWhereParams['wmctitle']."' loaded OK in folder '".$aWhereParams['folder']."'\n";
    echo "    for ".$aWhereParams['appname'].", $dbuser@$dbconnect\n";
}

// Generate a random password
function generatePassword()
{
    $length = 8;

    $password = "";
    $possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";

    $maxlength = strlen($possible);

    if ($length > $maxlength)
        $length = $maxlength;

    $i = 0; 
    while ($i < $length) 
    { 
      $char = substr($possible, mt_rand(0, $maxlength-1), 1);

      if (!strstr($password, $char)) 
      { 
        $password .= $char;
        $i++;
      }
    }

    return $password;
}

function getURL()
{
    //This will be URL
    return gethostname()."/apps/geoservices/";
}

function dir_walk($callback, $dir, $types = null, $recursive = false, $baseDir = '', $base)
{
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false)
        {
            if ($file === '.' || $file === '..')
            {
                continue;
            }
            if (is_file($dir . $file))
            {
                if (is_array($types))
                {
                    if (!in_array(strtolower(pathinfo($dir . $file, PATHINFO_EXTENSION)), $types, true))
                    {
                        continue;
                    }
                }
                $callback($base, $baseDir, $file);
            }
            elseif($recursive && is_dir($dir . $file))
            {
                dir_walk($callback, $dir . $file . DIRECTORY_SEPARATOR, $types, $recursive, $baseDir . $file . DIRECTORY_SEPARATOR, $base);
            }
        }
        closedir($dh);
    }
}
?>
此函数用于在给定连接字符串、用户和pwd的情况下创建Oracle数据库连接

function createDatabaseConnection($dbconnect, $dbuser, $dbpasswd) { // we need adodb require_once('/usr/share/php/adodb/adodb.inc.php'); // make sure that the OCI extension is loaded $szOCIModule = "php_oci8"; if (!extension_loaded("oci8")) dl($szOCIModule . "." . PHP_SHLIB_SUFFIX); $DB = NewADOConnection('oci8'); $DB->PConnect($dbconnect, $dbuser, $dbpasswd); if (!$DB) return false; if (!$DB->IsConnected()) return false; return $DB; }
函数createDatabaseConnection($dbconnect、$dbuser、$dbpasswd) { //我们需要adodb 需要一次('/usr/share/php/adodb/adodb.inc.php'); //确保已加载OCI扩展 $szOCIModule=“php_oci8”; 如果(!扩展插件加载(“oci8”)) dl($szOCIModule..”.PHP\u SHLIB\u后缀); $DB=NewADOConnection('oci8'); $DB->PConnect($dbconnect,$dbuser,$dbpasswd); 如果(!$DB)返回false; if(!$DB->IsConnected())返回false; 返回$DB; }
我的另一个脚本工作正常,与DBConnection相同。主要区别在于,在脚本中(有错误),我使用带有一些额外参数的函数handleFile。代码如下:

<?php include_once(dirname(__FILE__).'/../config/settings.php'); include_once(dirname(__FILE__).'/../htdocs/utils.php'); if ($argc < 3) { echo "Usage: insertwmc.php <application> <wmc file> [title] [folder]\n"; echo "\n"; echo "Inserts the wmc file as a CLOB into the userprofile database for a specific application\n"; echo "\n"; echo "The wmc file should be an absolute path.\n"; echo "\n"; echo "If no title is specified, the WMC will become the default WMC for the application\n"; echo "\n"; exit(1); } $aWhereParams = array(); $file=$argv[2]; $aWhereParams['appname'] = strtolower($argv[1]); $aWhereParams['wmctitle'] = null; $aWhereParams['folder'] = null; $aWhereParams['startwmc'] = 'Y'; if (isset($argv[3])) { $aWhereParams['wmctitle'] = $argv[3]; $aWhereParams['startwmc'] = 'N'; } if (isset($argv[4])) { $aWhereParams['folder'] = $argv[4]; } $aWhereParams['userid'] = null; $DB = createDatabaseConnection($dbconnect, $dbuser, $dbpasswd); if ($aWhereParams['folder'] !== null) { // check if folder is already created $sql = "SELECT FOLDERID FROM WMCFOLDERS WHERE FOLDERTITLE = :folder AND APPLICATION=:appname"; $ok = $DB->Execute($sql, $aWhereParams); if ($ok) { $myArray = $ok->GetArray(); if (empty($myArray)) { // insert the folder into WMCFOLDERS createWMCDirectory($DB, $aWhereParams); } else { foreach ($myArray as $row) { $aWhereParams['folderid'] = $row['FOLDERID']; } } } } else { $aWhereParams['folderid'] = null; } $folderIdValue; if ($aWhereParams['folderid'] === null) { $folderIdValue = 'IS NULL'; } else { $folderIdValue = '='.$aWhereParams['folderid']; } $titleValue; if ($aWhereParams['wmctitle'] === null) { $titleValue = 'IS NULL'; } else { $titleValue = '='.$DB->qstr($aWhereParams['wmctitle'], get_magic_quotes_gpc()); } // check if there is an existing entry, if so update it $sql = "SELECT APPLICATION FROM WMC WHERE STARTWMC=:startwmc AND APPLICATION=:appname AND FOLDERID ".$folderIdValue." AND TITLE ".$titleValue; $ok = $DB->Execute($sql, $aWhereParams); if ($ok) { $myArray = $ok->GetArray(); if (empty($myArray)) { // we need to insert a new entry $sql = "insert into WMC (USERID, APPLICATION, FOLDERID, TITLE, STARTWMC, WMC) values (null, :appname, :folderid, :wmctitle, :startwmc, null)"; $ok = $DB->Execute($sql, $aWhereParams); if (!$ok) { handleDBError($DB); } insertCLOB($DB, $file, $aWhereParams['appname'], $aWhereParams['folderid'] , $aWhereParams['wmctitle']); } else { insertCLOB($DB, $file, $aWhereParams['appname'], $aWhereParams['folderid'] , $aWhereParams['wmctitle']); } } else { handleDBError($DB); } echo "WMC '".$aWhereParams['wmctitle']."' loaded OK in folder '".$aWhereParams['folder']."'\n"; echo " for ".$aWhereParams['appname'].", $dbuser@$dbconnect\n"; ?>
从函数中可以看出,它很可能返回false

如果在
$DB=createDatabaseConnection(…)一行之后执行
var_dump($DB)
,会发生什么

如其他地方所述,您的createDatabaseConnection无法返回对象,可能是因为它通常会失败。您应该做的第一件事是查看该函数和可能返回的对象,尤其是在连接到数据库失败的情况下。在没有看到函数的情况下,它的返回状态是不清楚的,但它可能足以确保$DB不为NULL(编辑:从您现在发布的代码中,您应该检查它是否为false)


作为常规检查,您可以使用来检查您是否有对象。

您确定连接没有失败并返回
false
$DB
或其他什么吗?你应该有逻辑来解释连接失败。你能发布
createDatabaseConnection
的代码吗?@WiseGuy连接正常。我有其他使用相同连接的脚本,它们正在运行。@Luzhin我刚刚发布了它的代码createDatabaseConnection@SykWitIt连接正常吗?可能是这样,但请在代码中使用逻辑来证明这一点。;-)谢谢你的回答。我已经发布了createDatabaseConnection的代码。我试过is_object,我发现没有对象。@Luzhin hmmm…它给出:bool(false)!我不想说“我早就告诉过你了”,但是……然后就没有DB连接了。是的,这是我的结论;-)。但是我已经发布了另一个脚本,它使用相同的createDatabaseConnection函数,并且运行良好。也许我的剧本有问题。