function __construct(mysqli $db, $country = NULL, $sport = NULL) {
$this->db = $db;
$this->country = base64_decode($country);
$this->sport = base64_decode($sport);
}
public function GetColor($colorcode) {
$query = 'SELECT naam_nl FROM colors WHERE code = $colorcode';
$result = $this->db->query($query);
while ($row = $result->fetch_assoc()) { // Line 21
echo $row['naam_nl'];
}
$result->close();
}
给我:
Fatal error: Call to a member function fetch_assoc() on a non-object in /home/cloud/public/td/teamdresser.class.php on line 21
所以我试过了:
$result = $this->db->query($query);
while ($row = $this->db->fetch_assoc($result)) {
echo $row['naam_nl'];
}
然后......
Fatal error: Call to undefined method mysqli::fetch_assoc() in /home/cloud/public/td/teamdresser.class.php on line 21
我做错了什么..有人能指出我正确的方向吗?
答案 0 :(得分:1)
这部分错了:
public function GetColor($colorcode) {
$query = 'SELECT naam_nl FROM colors WHERE code = $colorcode';
使用单引号时,PHP不会插入$colorcode
。使用双引号intead:
$query = "SELECT naam_nl FROM colors WHERE code = $colorcode";
<强>评论强>:
始终检查返回值!这使得发现错误变得更加容易。
为什么不使用准备好的陈述?
答案 1 :(得分:1)
就像评论所说,你需要做一些错误检查:
public function GetColor($colorcode) {
$query = 'SELECT naam_nl FROM colors WHERE code = $colorcode';
$result = $this->db->query($query);
if ($result === false) {
// Throw or handle an error here
} else {
while ($row = $result->fetch_assoc()) { // Line 21
echo $row['naam_nl'];
}
$result->close();
}
}
此外,您还需要mysqlnd驱动程序:Fatal error: Call to undefined method mysqli_result::fetch_all()