Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
要使用 PDO 与 MySQL 数据库交互,假设命名占位符,需要一些数据库结构的知识:
$db->("INSERT INTO table (firstname, lastname, age) value (:fn, :ln, :age)");
有没有一种方法可以在没有此类知识的情况下使用带有命名占位符的准备好的语句,从而允许更改表结构而无需更改代码。或者,有没有办法查询数据库的表结构?
一个解决方案是观察你的商业模式:
select column_name, column_type from information_schema.columns where table_schema = 'yourschema' and table_name = 'yourtable';
解析这个结果,并使用 php.ini 即时构建插入查询。使用这些column_type信息,您可以在绑定值时映射 PDO::XXX 数据类型。
column_type