OOP php问题没有任何错误但没有打印数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OOP php问题没有任何错误但没有打印数据相关的知识,希望对你有一定的参考价值。
我写了一个类,用于从数据库(mysql)获取有关我的客户端的一些信息。我们可以在下面看到:
<?php
class Player {
public $username;
public $inf;
public function __construct ($username, $inf){
$this->username = $username;
$this->inf = $inf;
}
public function getInfuser (){
include('connectdatabase.php');
$sql = "SELECT * FROM member WHERE email = '.$this->username.'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
switch ($this->inf){
case ('email'):
$result = $row["email"];
break;
case ('playerid'):
$result = $row["playerid"];
break;
case ('inviteid'):
$result = $row["inviteid"];
break;
case ('hash'):
$result = $row["hash"];
break;
default:
$result = "Error";
break;
}
return $result;
}
}
?>
在connectdatabase.php
页面,我写了一个连接到我的数据库,我正确测试了它的工作。
我想从我的班级打印信息的页面在这里:
<?php
include ('classtest.php');
$username = 'chance';
$inf = 'inviteid';
$keyvan = new Player($username, $inf);
echo $keyvan->getInfuser ();
?>
我修复了一些语法错误。
[18-Dec-2018 20:14:52 UTC] PHP Parse error: syntax error, unexpected 'function__construct' (T_STRING), expecting variable (T_VARIABLE) in /home/jokerpoker021/public_html/classtest.php on line 8
[18-Dec-2018 20:14:52 UTC] PHP Parse error: syntax error, unexpected 'function__construct' (T_STRING), expecting variable (T_VARIABLE) in /home/jokerpoker021/public_html/classtest.php on line 8
[18-Dec-2018 20:14:53 UTC] PHP Parse error: syntax error, unexpected 'function__construct' (T_STRING), expecting variable (T_VARIABLE) in /home/jokerpoker021/public_html/classtest.php on line 8
[18-Dec-2018 20:16:34 UTC] PHP Fatal error: Call to a member function query() on null in /home/jokerpoker021/public_html/classtest.php on line 17
[18-Dec-2018 20:16:35 UTC] PHP Fatal error: Call to a member function query() on null in /home/jokerpoker021/public_html/classtest.php on line 17
现在没有任何错误,但我的信息不打印,只是显示一个白页,我不知道为什么。我的代码有什么问题吗?
更新:我的connecttodatabase.php
文件:
<?php
$servenm = "localhost";
$usnme = "username";
$passnm = "********";
$dbname = "jokerpok";
// Create connection
$conn = new mysqli($servenm, $usnme, $passnm, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
答案
我想你应该写:
$sql = "SELECT * FROM member WHERE email = '".$this->username."'";
要么 :
$sql = "SELECT * FROM member WHERE email = '{$this->username}'";
以上是关于OOP php问题没有任何错误但没有打印数据的主要内容,如果未能解决你的问题,请参考以下文章
FPDF 错误:一些数据已经输出,无法发送 PDF 文件。尝试了一切,但没有任何帮助