0

我已经编写了一个php class在一个小脚本中使用的,以运行我喜欢的其他脚本中的任何查询。不会在公开场合或生产环境中使用,而且我知道这会带来巨大的安全问题!

我已经将其作为练习来了解类等...我似乎在代码中的某一特定行遇到问题,这导致某处出现错误。我认为这可能是因为我试图返回一个数组,并且我认为我没有在类中正确定义它。

$this->resultOfQuery = mysqli_fetch_array($this->out_Resource, MYSQLI_ASSOC));

这是整个代码。

<?php

class GetRandomRecord {

//Connection
    public $CUDBName;   
    public $CUHost;     
    public $CUUser;     
    public $CUPassword; 
    public $in_SQL;
    public $out_Resource;
    public $CULink;  

    public $message;

    public $errors = array();         // is this correct?
    public $resultOfQuery = array();  // is this correct?

/****************************************************************/
    public function setSQL($value){
        $this->in_SQL = $value;
        return $this->in_SQL; 
    }

/****************************************************************/
    public function setConnectionString($db,$host,$user,$password){

        $this->CUDBName   = $db;
        $this->CUHost     = $host;
        $this->CUUser     = $user;
        $this->CUPassword = $password;

    }

/****************************************************************/
    public function runSQL() {

        $this->CULink  = mysqli_connect( $this->CUHost , $this->CUUser , $this->CUPassword , $this->CUDBName);

        if (mysqli_connect_errno()) {
            $this->message = "Connection failed: ".mysqli_connect_error();
            return $this->message;
        }


        $this->out_Resource  =  mysqli_query($this->in_SQL , $this->CULink);


        if (!$this->out_Resource)
        {
            $this->errors['sql']      = $this->in_SQL; 
            $this->errors['eeDBName'] = $this->CUDBName;
            $this->errors['eeLink']   = $this->CULink;
            $this->errors['status']   = "false"; //There was a problem saving the data;

            mysqli_close($this->CULink);

            return json_encode($this->errors);
        }
        else
        {                   

        // success
            $this->resultOfQuery = mysqli_fetch_array($this->out_Resource, MYSQLI_ASSOC));
            mysql_close($this->CULink);     
            return $this->resultOfQuery;

        } // if (!mysql_query( $CUDBName , $sql , $CULink))



    }
/****************************************************************/

}//class

$recordGet = new getRandomRecord();

$recordGet->setConnectionString('databasename','localhost','username','password');

// select count from database
$tableName = "userList";

$countSQL = "select count(*) from $tableName";

$recordGet->setSQL($countSQL);

$result = $recordGet->runSQL();

print_r($result);

?>

你能帮我找出问题吗?

编辑:实际上我没有收到特定的错误消息。我有一个HTTP Error 500这通常意味着我的代码是 duff,我通过注释代码部分来缩小范围,直到找到导致它的行。

4

1 回答 1

0

你在第 64 行有一个额外的 close-paren。

$this->resultOfQuery = mysqli_fetch_array($this->out_Resource, MYSQLI_ASSOC));

该行应该是:

$this->resultOfQuery = mysqli_fetch_array($this->out_Resource, MYSQLI_ASSOC);
于 2013-05-16T10:40:05.627 回答