我想使用fpdf将mysql数据库转换为pdf,但是当我尝试从不同的文件调用函数从数据库中获取数据时,我总是失败,我已阅读了几篇类似问题的文章,但我仍然有一个错误消息, 这是我的代码:
database.php中
<?php
class Database{
private static $INSTANCE = null;
private $mysqli,
$HOST = 'localhost',
$USER = 'root',
$PASS = '',
$DBNAME = 'newproject';
public function __construct()
{
$this->mysqli = new mysqli( $this->HOST, $this->USER, $this->PASS, $this->DBNAME );
if( mysqli_connect_error() ){
die('Failed to connect');
}
}
public static function getInstance()
{
if( !isset( self::$INSTANCE ) ){
self::$INSTANCE = new Database();
}
return self::$INSTANCE;
}
public function get_info($table, $column = '', $value = '')
{
if( !is_int($value) )
$value = "'". $value . "'";
if( $column != '' ) {
$query = "SELECT * FROM $table WHERE $column = $value";
$result = $this->mysqli->query($query);
while($row = $result->fetch_assoc()){
return $row;
}
}else{
$query = "SELECT * FROM $table";
$result = $this->mysqli->query($query);
while($row = $result->fetch_assoc()){
$results[] = $row;
}
return $results;
}
}
?>
核心文件夹中的load.php
<?php
session_start();
spl_autoload_register(function($class){
require_once 'classes/' .$class. '.php';
});
$user = new User();
?>
User.php
<?php
class User{
private $_db;
public function __construct()
{
$this->_db = Database::getInstance();
}
public function get_asets()
{
return $this->_db->get_info('asets');
}
?>
这是我的pdf转换器代码
<?php
require_once 'core/load.php';
require_once 'report/fpdf.php';
class PrintAset extends FPDF
{
private $asets;
function __construct(){
$this->$asets = $user->get_asets();
}
function viewTable(){
$this->SetFont('Times','',8);
foreach ($asets as $aset) {
$this->MultiCell(25,10,$aset['data1'],1,'C');
$this->MultiCell(25,10,$aset['data2'],1,'C');
$this->MultiCell(25,10,$aset['data3'],1,'C');
$this->MultiCell(35,10,$aset['data4'],1,'C');
$this->MultiCell(35,10,$aset['data5'],1,'C');
$this->MultiCell(25,10,$aset['data6'],1,'C');
$this->MultiCell(35,10,$aset['data7'],1,'C');
}
}
}
$pdf = new PrintAset();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4','0');
$pdf->headerTable();
$pdf->viewTable();
$pdf->Output();
?>
当我在浏览器上测试时,这是返回的错误
注意:未定义的变量:用户输入 第11行的C:\ xampp \ htdocs_sikp \ cetakAset.php
致命错误:未捕获错误:调用成员函数get_asets() C:\ xampp \ htdocs_sikp \ cetakAset.php中的null:11堆栈跟踪:#0 C:\ xampp \ htdocs_sikp \ cetakAset.php(53):CetakAset-&gt; __ construct()#1 {main}在第11行的C:\ xampp \ htdocs_sikp \ cetakAset.php中抛出