[英]PHP PDO Fetch_Assoc not returning correctly
I have searched, and maybe I am searching for the wrong thing, but I need help with the below code. 我搜索过,也许我正在寻找错误的东西,但我需要以下代码的帮助。 I must be missing something...
我肯定错过了什么...
Database Class: 数据库类:
class Database {
private static $link = null;
private static function dbLink() {
if(self::$link) {
return self::$link;
}
$dsn = null;
switch(DB_DRVR) {
case 'mysql':
$dsn = DB_DRVR . ":host=" . DB_HOST . ";dbname=" . DB_NAME;
break;
}
self::$link = new PDO($dsn, DB_USER, DB_PASS);
return self::$link;
}
public static function fetchAssoc($sql) {
$stmt = self::query($sql);
$result = array();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result;
}
public static function __callStatic($name, $arguments) {
$callback = array(self::dbLink(), $name);
return call_user_func_array($callback, $arguments);
}
}
Portion of code returning error: 返回错误的代码部分:
$sql = "SELECT group FROM users WHERE username='ryan';";
$dbSQL = Database::fetchAssoc($sql);
echo $dbSQL['group'];
Database setup: 数据库设置:
- id(auto_increment, primary)
id(auto_increment,primary)
- name(tinytext)
名字(TINYTEXT)
- username(tinytext)
用户名(TINYTEXT)
- password(tinytext)
密码(TINYTEXT)
- salt(tinytext)
盐(TINYTEXT)
- email(tinytext)
电子邮件(TINYTEXT)
- group(tinytext)
组(TINYTEXT)
- privileges(tinytext)
特权(TINYTEXT)
I'm receiving the following error: 我收到以下错误:
Fatal error: Call to a member function fetch() on a non-object
in C:\public_html\kernel\database.php on line 38
However the odd part is if I change the SQL to: 但奇怪的是,如果我将SQL更改为:
$sql = "SELECT * FROM users WHERE username='ryan';";
I don't get the error, it displays properly, and if I change the SQL statement to: 我没有收到错误,它显示正确,如果我将SQL语句更改为:
$sql = "SELECT username FROM users WHERE username='ryan';";
and call $dbSQL['username'] it works fine using the function above. 并调用$ dbSQL ['username']使用上面的函数可以正常工作。
My Question: How come when I try to use the 'group' column in my $dbSQL array, I am receiving an error, but when I try to use the 'username' column, everything runs fine. 我的问题:为什么当我尝试在$ dbSQL数组中使用'group'列时,我收到一个错误,但是当我尝试使用'username'列时,一切运行正常。 What am I missing?
我错过了什么?
Thanks, Ryan 谢谢,瑞恩
SELECT `group` FROM users WHERE username='ryan'
GROUP
is a reserved keyword in SQL, your query produces an error if you use it. GROUP
是SQL中的保留关键字,如果使用它,则查询会产生错误。 Quote it: 引用它:
SELECT `group` ...
It sounds to me as if your query is failing because your RDBMS reserves the word group
. 听起来好像你的查询失败了,因为你的RDBMS保留了单词
group
。
To avoid this type of problem in future: 为了避免将来出现这种问题:
Implement some error checking and you would see a clue from the DB server about which part of which query fails. 实现一些错误检查,您将从数据库服务器中看到有关哪个部分查询失败的线索。 There are a few ways you can do this:
有几种方法可以做到这一点:
self::errorInfo()
after running each query, either printing or logging the output. self::errorInfo()
,打印或记录输出。 PDO::ERRMODE_EXCEPTION
. PDO::ERRMODE_EXCEPTION
。 See the manual page about Errors and error handling
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.