Mike Kellogg
Mike Kellogg

Reputation: 1178

using class method to pull database info

Overview: I have a function that is supposed to pull a row of the db based on its id number

Problem: It seems my function is returning, but it isn't returning anything.

Details:

-I have 2 different files used here: db_class.php and character_pull.php

-Also I have a database that contains 1 table (characters) that does contain column "id"

-there are echo lines for debugging. I will give what the output is.

character_pull.php:

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

db_class.php:

<?php

include "database_connect.php";

class db_class{



    public function getPlayerStats($id){

        echo "<br/>" . "making it in class1";

        $query = "SELECT * FROM characters WHERE id = $id";
        $result = mysqli_query($query);

        return $char = mysqli_fetch_array($result);


            $result ->close();
    }

}

?>

the output I receive when I run the page is this:

made it out here1made it out here2 making it in class1made it out here3 made it out here4

I have tried several things to fix this, but am having trouble figuring out what is wrong.

I know that this is probably extremely sloppy and primitive, but try not to laugh too hard and maybe you can help me out :P. Thanks in advance.

Upvotes: 0

Views: 95

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

You have a number of issues here.

It seems your DB class is quite incomplete. To me, if I am creating a class to represent a DB connection and various operations I am going to make that connection in that class, not via some include (where I assume the connection is happening). The here is that the include will only occur conditionally if your code hits that line. In this case, since you have that include outside any actual function in the class (like a constructor) it will never be called.

I would suggest something like this to resolve this:

class db_class {
    protected $mysqli;
    private $db_host = 'your_db_host';
    private $db_user = 'your_db_user';
    private $db_password = 'your_db_password';
    protected $db_name = 'default_db_name';

    public __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

        $mysqli = new mysqli($this->db_host, $this->db_use, $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;
        }
    }

    // other class methods
}

You now have an object representing a mysqli connection store in $this->mysqli.

Your getPlayerStats() method might now look like

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;
    }
}

Note I used prepared statements here, which, you should get used to using as it is really best practice for querying databases. Note also I have added in handling of error cases all throughout the code. You should get in the habit of doing this, as it will making debugging much easier.

Upvotes: 2

Nicholas Smith
Nicholas Smith

Reputation: 456

Just a guess but I would move this above the class name:

<?php
include "database_connect.php";
    class db_class{

Upvotes: 0

Related Questions