pdo 连接类? [英] pdo connection class?
问题描述
我正在尝试了解 PDO,人们说它是与数据库交互的简单和最佳方式,但我觉得这并不容易.有什么可以帮忙的吗?
I am tring to learn about PDO people have said its easy and the best way to interact with the database, but im not finding it easy. can any help?
连接.php
<?php
class Connection {
public function dbConnect() {
return new PDO("mysql:host=localhost; dbname=lr", "root", "");
}
}
?>
index.php
<?php
require_once('pdo.php');
$obj = new Connection();
$obj->dbConnect();
echo 'hello';
?>
这行得通,但太简单了.什么是最好的方法?
This works but its too simple. what is the best way?
我应该在自己的类中建立连接并将其扩展到我的其他类,还是应该使用全局变量来连接?还是有另一种更好的方法?谢谢
Should i have my connection in its own class and extend it to the rest of my classes or should i use a global variable to connect? or is there another way better way? Thanks
推荐答案
您只是在创建连接,并没有告诉我们您要实现的目标.您是要发布、更新、删除数据库中的某些内容还是什么?
You are just creating a connection and not telling us what you are trying to achieve. Do you want to post, update, delete something in the database or what?
因为您什么都不问,所以我能做的最好的事情就是将您链接到一个网站,该网站将尝试教您基础知识.不过我希望你以前用过数据库.
The best I can do since you aren't asking anything is link you to a website that will try to teach you the basics. I hope you have used databases before though.
http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/
这篇关于pdo 连接类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!