PHP致命错误:在非对象上调用成员函数Exec​​ute()

时间:2011-06-17 13:04:12

标签: php

当我执行此代码时,我收到一条错误消息“PHP致命错误:在非对象上调用成员函数Exec​​ute()”。导致此错误的行是第96行:$ ok = $ DB->执行($ sql,$ aWhereParams);我试图解决这个问题,但我没有成功。可能代码不返回对象?如何捕获此错误或我的代码有问题?请帮忙。谢谢!

SykWitIt

<?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);
    }
}
?>

This the function that creates an Oracle database connection given a connect string, user and pwd

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;
}

My other script works fine, the same DBConnection. The main difference is that in the script(with the error) i use function handleFile with some extra params. The code is here:

<?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);
    }
}
?>

2 个答案:

答案 0 :(得分:1)

从函数中可以看出,它最有可能返回false。

如果您在var_dump($DB)

行后面$DB = createDatabaseConnection(...);会发生什么情况

答案 1 :(得分:1)

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

作为一般检查,您可以使用is_object检查您是否有对象。