PHP如何定义数组并在类中引用它?
我已经编写了一个PHP如何定义数组并在类中引用它?,php,arrays,class,Php,Arrays,Class,我已经编写了一个php类,在一个小脚本中使用它来运行我喜欢的来自其他脚本的任何查询。这是而不是将被公开使用或在生产中使用,我知道这会带来巨大的安全问题 我做这个练习是为了了解课程等。。。我似乎对代码中的某一行有问题,这导致了某个地方的错误。我想这可能是因为我试图返回一个数组,但我认为我没有在类中正确定义它 $this->resultOfQuery = mysqli_fetch_array($this->out_Resource, MYSQLI_ASSOC)); 这就是全部代码 &l
php类
,在一个小脚本中使用它来运行我喜欢的来自其他脚本的任何查询。这是而不是将被公开使用或在生产中使用,我知道这会带来巨大的安全问题
我做这个练习是为了了解课程等。。。我似乎对代码中的某一行有问题,这导致了某个地方的错误。我想这可能是因为我试图返回一个数组,但我认为我没有在类中正确定义它
$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);
?>
第64行有一个额外的关闭参数
$this->resultOfQuery = mysqli_fetch_array($this->out_Resource, MYSQLI_ASSOC));
线路应为:
$this->resultOfQuery = mysqli_fetch_array($this->out_Resource, MYSQLI_ASSOC);
“…这在某处导致了错误…”show error text错误消息显示的是什么..事实上,这是一段需要仔细梳理的代码,因此任何其他细节,如实际的错误消息,都会更有帮助。检查您的错误_logs@PhantomVineet1982 Romi Halasz我已经更新了帖子。