简体   繁体   中英

DB pulling Function with PHP

I am very new to programming with PHP and am working on a fun little game to help myself learn. I got some code help from others on pulling a character's stats from the DB but am having trouble getting it to work. I just get "server error" when I try to run it right now. The Database information is fine, and I previously had a working function that pulled from the DB, but wanted to universalize it through a class function. Here is what I have so far.

DB class:

<?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 class function call:

<?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";
?>

It is difficult to debug this code since I'm used to just putting in breaklines and running through coding errors in things like VBA in compilers, so any debugging tips would be greatly helpful. What am I doing wrong here? Thanks in advance!

You wrote

public __construct($db_host = NULL, ...

but constructors are functions. You need

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

Your db_class constructor accepts four parameters. This instantiation passes none.

$classobject = new db_class();

So you end up with junk in your connection string. Sort that out and you'll be on your way.


You can avoid a lot of debugging by building minimal versions that work. For example, you can write this first.

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

If that works, check it in to version control, then add a little code to it. (How do you know whether it works? Test it.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM