OOP php问题没有任何错误但没有打印数据

问题描述 投票:-2回答:1

我写了一个类,用于从数据库(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);
}     
?>
php oop
1个回答
0
投票

我想你应该写:

$sql = "SELECT * FROM member WHERE email = '".$this->username."'";

要么 :

$sql = "SELECT * FROM member WHERE email = '{$this->username}'";
© www.soinside.com 2019 - 2024. All rights reserved.