我是PHP的新手,想知道为什么这段代码没有向DB中插入任何内容(返回0)。我确定这一定是逻辑错误,因为我没有收到任何错误消息。
class DbConnection {
protected $db_conn;
public $db_host = "localhost";
public $db_name = "todo";
public $db_user = "root";
public $db_pass = "";
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_connecton = new DbConnection ();
$this->link = $db_connecton->connect ();
return $this->link;
}
function registerUsers ($username, $password, $ip_address, $reg_time, $reg_date) {
$query = $this->link->prepare("INSERT INTO users (username, password, ip_address, reg_time, reg_date) VALUES (?,?,?,?,?)");
$values = array ($username, $password, $ip_address, $reg_time, $reg_date);
$query->execute ($values);
$counts = $query->rowCount ();
return $counts;
}
}
$users = new ManageUsers ();
echo $users->registerUsers ("bob", "lassie", "127.0.0.2", "13:37", "03-14-15");
您不应该在PDO
连接中放置空格,否则将承担奇怪的事情,例如,它认为您的数据库名称是:[space]todo
等等,因此只需删除连接字符串中的每个空格,如下所示:
$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass);
//^ ^ ^ ^no spaces
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句