DB使用PHP提取功能

时间:2013-08-28 03:54:00

标签: php html

我对使用PHP进行编程非常陌生,正在开发一款有趣的小游戏来帮助自己学习。我从其他人那里获得了一些代码帮助,从数据库中提取角色的统计数据,但是无法让它工作。当我尝试立即运行它时,我只是得到“服务器错误”。数据库信息很好,我之前有一个从数据库中提取的工作函数,但希望通过类函数对其进行通用化。这是我到目前为止所拥有的。

数据库类:

<?php

class db_class
{
    //db connection portion
    protected $mysqli;
    private $db_host = 'XXXXXXX';
    private $db_user = 'Filler';
    private $db_password = 'Filler';
    protected $db_name = 'Filler';

    //db connection portion    
    public function __construct($db_host = null, $db_user = null, $db_password = null, $db_name = null) {
        if (!empty($db_host)) {
            $this->db_host = $db_host;
        }

        // validate other parameters similarly

        //database connection object
        $mysqli = new mysqli($this->db_host, $this->db_user, $this->db_password, $this->db_name);

        if ($mysqli->connect_error) {
            throw new Exception('Connect Error: ' . $mysqli->connect_errno . ', ' . $mysqli->connect_error);
        } else {
            $this->mysqli = $mysqli;
        }
    }

    public function getPlayerStats($id) {    
        if (empty($id)) {
            throw new Exception ('An empty value was passed for id');
        }
        // verify this is integer-like value
        $id      = (string) $id;
        $pattern = '/^\d+$/';
        if (!preg_match($pattern, $id) !== 1) {
            throw new Exception ('A non-integer value was passed for id');
        }
        $id = (int) $id;

        $query = "SELECT id, name, strength, defense, level, health, type, experience FROM characters WHERE id = :id";
        $stmt  = $this->mysqli->prepare($query);
        $stmt->bind_param('i', $id);
        $result = $stmt->execute();

        if (false === $result) {
            throw new Exception('Query error: ' . $stmt->error);
        } else {
            $obj = new stdClass();
            $stmt->bind_result($obj->id, $obj->name, $obj->strength, $obj->defense, $obj->level, $obj, health, $obj->type, $obj->experience);
            $stmt->fetch();
            $stmt->close();

            return $obj;
        }
    }
}
?>

DB类函数调用:

<?php
include "db_class.php";

echo "made it out here1";

$classobject = new db_class();
echo "made it out here2";

$results = $classobject->getPlayerStats('1');

print_r($results);

echo "made it out here3";

$id         = "id: " . $results['id'];
$name       = "name: " . $results['charname'];
$strength   = "strength: " . $results['strength'];
$defense    = "defense: " . $results['defense'];
$health     = "health: " . $results['health'];
$level      = "level: " . $results['level'];
$type       = "type: " . $results['type'];
$experience = "experience: " . $results['experience'];

echo "<br/>";

echo "made it out here4";
?>

很难调试这段代码,因为我习惯于在编译器中插入断点线并运行VBA之类的编码错误,所以任何调试技巧都会非常有用。我在这做错了什么?提前谢谢!

1 个答案:

答案 0 :(得分:3)

你写了

public __construct($db_host = NULL, ...

但构造函数是函数。你需要

public function __construct($db_host = NULL, ...

您的db_class构造函数接受四个参数。这个实例化没有通过。

$classobject = new db_class();

所以你最终会在你的连接字符串中使用垃圾。排除它,你将在路上。


您可以通过构建有效的最小版本来避免大量调试。例如,您可以先写这个。

<?php
class db_class{
  public function __construct($db_host = NULL, $db_user = NULL, $db_password = NULL, $db_name = NULL) {
  }
} 
?>

如果可行,请将其签入版本控制,然后为其添加一些代码。 (你怎么知道它是否有效?测试它。)