Patryk Stefański
- Super Gaduła
- 788 94 127 65
Ogólnie nastukałem szybko jakąś klasę odpowiadającą za baze danych i podczas testów wyskoczył mi błąd
Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in E:\xampp\htdocs\zqscript\pages\class\database.class.php:82
Linie 82 zaznaczyłempublic function execute() // LINIA 82
Klasa Db
Db.php
Home.php
Linie 82 zaznaczyłem
Klasa Db
<?php
/*
@Database Class
@Version 1.0
@Author ZnaQu
*/
class Database {
/**
* @var db_host
*/
private $db_host;
/**
* @var db_name
*/
private $db_name;
/**
* @var db_user
*/
private $db_user;
/**
* @var db_password
*/
private $db_password;
/**
* @var dbh
*/
private $dbh;
public function __construct($db_host, $db_name, $db_password, $db_user)
{
try {
$dns = "mysql:host=" . $this->db_host . ";dbname=" . $this->db_name;
$options = [
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$this->dbh = new PDO($dns, $this->db_user, $this->db_password, $options);
}
catch(PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
/**
* @var stmt
*/
private $stmt;
public function query($query)
{
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null)
{
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute() // LINIA 82
{
return $this->stmt->execute();
}
public function resultset()
{
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single()
{
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}
<?php
require_once CLASSS."database.class.php";
$db = new Database("localhost", "zqscript","123456","root");
<?php
require_once PAGES. "database.php";
$query = "SELECT * FROM _news";
$stmt = $db->query($query);
$stmt = $db->execute();
$smarty->assign("result", $stmt);
$smarty->display("home.tpl");