Answer the question
In order to leave comments, you need to log in
OOP class, for PDO authorization?
Hello,
Help me resolve the issue of whether the connection to the database should be wrapped in a class (ConnectionBD, for example) or you can simply put it in a separate file.
How to organize the correct connection to the database?
I did the following at this stage, before there was a normal authorization through MD5 and a connection from a file. Implemented on the example of the article phpfaq.ru/pdo.
define('DB_HOST', 'localhost');
define('DB_NAME', 'sadwordsbd');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_CHAR', 'utf8');
class Connection {
//переменная для проверки
protected static $instance = null;
public static function instance() {
if (self::$instance === null)
{
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => TRUE,
);
$db_Con = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHAR;
self::$instance = new PDO($db_Con, DB_USER, DB_PASS, $opt);
}
return self::$instance;
}
}
Сам запрос
require_once("class.php");
$dbh = new Connection;
$sql="
SELECT * FROM `UsersSW` WHERE `username`='123';
";
$stmt = $db->query($sql);
if(!$stmt){echo('Error selecting news item:'.$mysqli_error());exit();}
Answer the question
In order to leave comments, you need to log in
You can use OOP - write an add-on over PDO.
You can, of course, write a bicycle ...
Or you can use an object-relational mapping for building: ORM.
Or you can take a ready-made solution that is used in Symfony frameworks, ZendFramewok:
Doctrine 2 Object Relational Mapper (ORM)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question