概述:我有一个函数,应该根据其 ID 号提取数据库的一行
问题:我的函数似乎正在返回,但它没有返回任何内容。
详细信息:
-我这里使用了 2 个不同的文件:db_class.php 和 character_pull.php
-我还有一个数据库,其中包含 1 个包含“id”列的表(字符)
-有用于调试的回显线。我会给出输出是什么。
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();
}
}
?>
运行页面时收到的输出是这样的:
made it out here1made it out here2 making it in class1made it out here3 made it out here4
我已经尝试了多种方法来解决此问题,但无法找出问题所在。
我知道这可能非常草率和原始,但尽量不要笑得太厉害,也许你可以帮助我:P。提前致谢。
最佳答案
您遇到了很多问题。
看来你的数据库类很不完整。对我来说,如果我要创建一个类来表示数据库连接和各种操作,我将在该类中建立该连接,而不是通过某些包含(我假设连接正在发生)。这里的意思是,只有当您的代码命中该行时,包含才会有条件地发生。在这种情况下,由于您在类中的任何实际函数(如构造函数)之外包含了它,因此它永远不会被调用。
我建议像这样的事情来解决这个问题:
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
}
您现在在 $this->mysqli
中有一个代表 mysqli 连接存储的对象。
您的 getPlayerStats()
方法现在可能看起来像
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;
}
}
注意我在这里使用了准备好的语句,您应该习惯使用它,因为它确实是查询数据库的最佳实践。另请注意,我在整个代码中添加了对错误情况的处理。您应该养成这样做的习惯,因为这将使调试变得更加容易。
关于php - 使用类方法提取数据库信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16366501/