好吧所以我刚刚开始了一个项目,我创建了网站的会员资格部分我有2个类到目前为止一个用于数据库,其他用于管理这里的用户:
class dbConnect
{
protected $db_conn;
public $db_name = 'todo';
public $db_user= 'root';
public $db_pass = 'pass';
public $db_host = '127.0.0.1';
function connect()
{
try
{
$this->db_conn = new PDO("mysql:host=$this->db_host;dbname = $this->db_name",$this->db_user,$this->db_pass);
return $this->db_conn;
}
catch(PDOException $e)
{
return $e->getMessage();
}
}
}
这是用户类
class ManageUsers
{
public $link;
function __construct()
{
$db_connection = new dbConnect();
$this->link = $db_connection->connect();
return $this->link;
}
function registerUsers($username,$pass,$ip_adress,$time,$date)
{
$query = $this->link->prepare("INSERT INTO `users` (username, password, ip_adress, time, date_joined) VALUES (?,?,?,?,?)");
$values = array($username,$pass,$ip_adress,$time,$date);
$query->execute($values);
$counts = $query->rowCount();
return $counts;
}
}
$users = new ManageUsers();
echo $registered = $users->registerUsers('bob','marley','127.0.0.1','12:00','29-02-2012');
现在我遇到的问题是它没有将数据插入数据库中我已经三次检查并且数据库中的表与查询匹配,所以它可能是语法中的东西。该函数运行时没有错误,它返回零而不是1.非常感谢帮助,方便
答案 0 :(得分:1)
嗯,有几个明显的问题:
当SQL命令/查询失败时停止执行是一个好习惯。要激活它,您需要定义相应的驱动程序选项PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
dbConnect
课程没有意义首先,您正在连接到MySQL
服务器,因此以某种方式表示它会好得多。由于您的dbConnect
是数据库连接的逻辑部分,composition不是可行的方法。而应该扩展该类(因为它满足is-a
关系)
所以,你可以编写一个特定于MySQL-PDO的类,它看起来像这样:
class MySQL_PDO extends PDO
{
public function __construct(array $params)
{
$dsn = 'mysql:host=' . $params['host'];
if (isset($params['dbname'])) {
$dsn .= ';dbname=' . $params['dbname'];
}
$options = array(
parent::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8',
parent::ATTR_ERRMODE => parent::ERRMODE_EXCEPTION,
parent::ATTR_EMULATE_PREPARES => false,
parent::ATTR_DEFAULT_FETCH_MODE => parent::FETCH_ASSOC,
);
parent::__construct($dsn, $params['username'], $params['password'], $options);
}
}
你可以像
一样初始化它$db_options = array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'dbname' => 'todo'
);
$pdo_driver = new MySQL_PDO($db_options);
您的ManageUsers
实际上是DataMapper的实施,应该正确利用Dependency Injection
另外,请注意time
是MySQL中的保留字,因此您最好在任何地方坚持使用反向标记。
class UserMapper
{
protected $pdo;
public function __construct($pdo)
{
$this->pdo = $pdo;
}
public function insert($username, $password, $ip_adress, $time, $date_joined)
{
$query = "INSERT INTO `users` (`username`, `password`, `ip_adress`, `time`, `date_joined`) VALUES (:username, :password, :ip_adress, :time)"
$stmt = $this->pdo->prepare($query);
return $stmt->execute(array(
':username' => $username,
':password' => $password,
':ip_adress' => $ip_adress,
':date_joined'=> $date_joined
));
}
}
最后,你可以使用它,就像:
$mapper = new UserMapper($pdo_driver);
$mapper->insert('foo', .....);
答案 1 :(得分:1)
这是工作代码
class dbConnect
{
protected $db_conn;
public $db_host = '127.0.0.1';
public $db_user = 'root';
public $db_pass = 'roadmin';
public $db_name = 'todo';
public function connect()
{
try
{
$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user,$this->db_pass);
return $this->db_conn;
}
catch(PDOException $e)
{
return $e->getMessage();
}
}
}
以下是管理用户类
include_once('class.db.php');
class ManageUsers
{
public $link;
function __construct()
{
$db_connection = new dbConnect();
$this->link = $db_connection->connect();
return $this->link;
}
function registerUsers($username,$email,$password,$ip_adress,$date)
{
$query = $this->link->prepare("INSERT INTO `users` (username,email, password, ip_adress, date_joined) VALUES(?,?,?,?,?)");
$values = [$username,$email,$password,$ip_adress,$date];
$query->execute($values);
$confirm = $query->rowCount();
return $confirm;
}
function loginUser
}
$test = new ManageUsers();
echo $test->registerUsers('bob','a@a.com','lol','127.0.0.1','2012');