调用未定义的方法 Database::prepare()
来自 http://www.php.net/manual/en/class.pdo .php
###### config.ini ######
db_driver=mysql
db_user=root
db_password=924892xp
[dsn]
host=localhost
port=3306
dbname=localhost
[db_options]
PDO::MYSQL_ATTR_INIT_COMMAND=set names utf8
[db_attributes]
ATTR_ERRMODE=ERRMODE_EXCEPTION
############
<?php class Database {
private static $link = null ;
private static function getLink ( ) {
if ( self :: $link ) {
return self :: $link ;
}
$ini = _BASE_DIR . "config.ini" ;
$parse = parse_ini_file ( $ini , true ) ;
$driver = $parse [ "db_driver" ] ;
$dsn = "${driver}:" ;
$user = $parse [ "db_user" ] ;
$password = $parse [ "db_password" ] ;
$options = $parse [ "db_options" ] ;
$attributes = $parse [ "db_attributes" ] ;
foreach ( $parse [ "dsn" ] as $k => $v ) {
$dsn .= "${k}=${v};" ;
}
self :: $link = new PDO ( $dsn, $user, $password, $options ) ;
foreach ( $attributes as $k => $v ) {
self :: $link -> setAttribute ( constant ( "PDO::{$k}" )
, constant ( "PDO::{$v}" ) ) ;
}
return self :: $link ;
}
public static function __callStatic ( $name, $args ) {
$callback = array ( self :: getLink ( ), $name ) ;
return call_user_func_array ( $callback , $args ) ;
}
} ?>
<?php // examples
$stmt = Database :: prepare ( "SELECT 'something' ;" ) ;
$stmt -> execute ( ) ;
var_dump ( $stmt -> fetchAll ( ) ) ;
$stmt -> closeCursor ( ) ;
?>
我做错了什么,或者这个示例代码是错误的? 调用第 167 行未定义的方法 Database::prepare()
from http://www.php.net/manual/en/class.pdo.php
###### config.ini ######
db_driver=mysql
db_user=root
db_password=924892xp
[dsn]
host=localhost
port=3306
dbname=localhost
[db_options]
PDO::MYSQL_ATTR_INIT_COMMAND=set names utf8
[db_attributes]
ATTR_ERRMODE=ERRMODE_EXCEPTION
############
<?php class Database {
private static $link = null ;
private static function getLink ( ) {
if ( self :: $link ) {
return self :: $link ;
}
$ini = _BASE_DIR . "config.ini" ;
$parse = parse_ini_file ( $ini , true ) ;
$driver = $parse [ "db_driver" ] ;
$dsn = "${driver}:" ;
$user = $parse [ "db_user" ] ;
$password = $parse [ "db_password" ] ;
$options = $parse [ "db_options" ] ;
$attributes = $parse [ "db_attributes" ] ;
foreach ( $parse [ "dsn" ] as $k => $v ) {
$dsn .= "${k}=${v};" ;
}
self :: $link = new PDO ( $dsn, $user, $password, $options ) ;
foreach ( $attributes as $k => $v ) {
self :: $link -> setAttribute ( constant ( "PDO::{$k}" )
, constant ( "PDO::{$v}" ) ) ;
}
return self :: $link ;
}
public static function __callStatic ( $name, $args ) {
$callback = array ( self :: getLink ( ), $name ) ;
return call_user_func_array ( $callback , $args ) ;
}
} ?>
<?php // examples
$stmt = Database :: prepare ( "SELECT 'something' ;" ) ;
$stmt -> execute ( ) ;
var_dump ( $stmt -> fetchAll ( ) ) ;
$stmt -> closeCursor ( ) ;
?>
What have I done wrong, or was this example code wrong?
Call to undefined method Database::prepare() on line 167
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的数据库类中没有方法
prepare
。它属于您存储为$link
的 PDO 对象。您可能正在寻找的是
更新您的评论:您是对的,没有明白这一点。
__callStatic
可在 PHP >= 5.3 中使用仅.0 - 您有正确的版本吗?There is no method
prepare
in your Database class. That belongs to the PDO object that you are storing as$link
.What you are probably looking for is
Update re your comment: You're right, didn't catch that.
__callStatic
is available in PHP >= 5.3.0 only - do you have the right version?正如错误消息所示:数据库类中有 noch 名为“prepare”的公共静态方法...
as the error message says: there is noch public static method named "prepare" in you database-class...