我还有另一个脚本'DBSetting.php',在其中分配连接到MySQL数据库所需的数据。 但是,当我尝试编写“ fetchAll();”时,如何使脚本准确地调用函数?最后,它显示了以下错误:
PHP解析错误:语法错误意外出现在'fetchAll'(T_STRING),期望函数(T_FUNCTION)或const(T_CONST)中 第x行上的/path/to/script/DBclass.php
<?php
require_once( 'DBSettings.php' );
class DBClass extends DatabaseSettings
{
var $classQuery;
var $link;
var $errno = '';
var $error = '';
// Connects to the database
function DBClass()
{
$settings = DatabaseSettings::getSettings();
$host = $settings['dbhost'];
$name = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
$this->link = new mysqli( $host , $user , $pass , $name );
}
function fetchAll( $result , $resultType = MYSQLI_ASSOC )
{
return $result->fetch_all( $resultType );
}
}
?>
我只想得到一些输出,如果我通过PHP DBclass.php在每个命令行中运行脚本,它不会给我一个错误,并且行为正常->因为它只包含功能
答案 0 :(得分:0)
不确定它是否理解您要实现的目标,所以有两件事:
您可以通过执行以下操作(未经测试!)来调用fetchAll()
方法:
<?php
require_once( 'DBSettings.php' );
class DBClass extends DatabaseSettings
{
var $classQuery;
var $link;
var $errno = '';
var $error = '';
// Connects to the database
function __construct()
{
$settings = DatabaseSettings::getSettings();
$host = $settings['dbhost'];
$name = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
$this->link = new mysqli( $host , $user , $pass , $name );
}
function query( $link )
{
return mysqli_query($link, 'SELECT * FROM your_table');
}
function fetchAll( $result , $resultType = MYSQLI_ASSOC )
{
return $result->fetch_all( $resultType );
}
}
?>
在另一个文件中:
$db_class = new DBClass();
$query = $db_class->query($db_class->link);
var_dump($db_class->fetchAll($query));