PHP如何定义数组并在类中引用它?

时间:2013-05-16 09:56:48

标签: php arrays class

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

我已经做了这个作为练习,看到了解类等...我似乎遇到了代码中某个特定行的问题,导致某个地方出现错误。我想这可能是因为我正在尝试返回一个数组,而且我认为我没有在课堂上正确定义它。

$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,我通过注释代码段来缩小它,直到我找到导致它的行。

1 个答案:

答案 0 :(得分:0)

你在第64行有一个额外的近距离。

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

该行应为:

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