-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMysql.php
More file actions
62 lines (60 loc) · 1.88 KB
/
Mysql.php
File metadata and controls
62 lines (60 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
class Mysql
{
protected $conexion;
private $usuario;
private $password;
private $dbname;
private $hostname;
function __construct($host_name,$db_name,$user,$contraseña){
$this->hostname = $host_name;
$this->dbname = $db_name;
$this->usuario = $user;
$this->password = $contraseña;
try{
$this->conexion = new PDO('mysql:hostname='.$this->hostname,$this->usuario,$this->password);
}catch(PDOException $e){
echo $e->getMessage();
die();
}
$this->setupDatabase();
}
public function execStatementSetter($stm,$placeholders){
$statement = $this->conexion->prepare($stm);
if($placeholders != false){
$statement->execute($placeholders);
}else{
$statement->execute();
}
}
public function execStatementGetter($stm,$placeholders,$fetchall){
$statement = $this->conexion->prepare($stm);
if($placeholders != false){
$statement->execute($placeholders);
}else{
$statement->execute();
}
if($fetchall == true){
$resultado = $statement->fetchAll();
}else{
$resultado = $statement->fetch();
}
return $resultado;
}
private function setupDatabase(){
$stm = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$this->dbname'";
$statement = $this->conexion->prepare($stm);
$statement->execute();
if(((bool) $statement->fetchColumn()) == false){
$this->execStatementSetter("CREATE DATABASE IF NOT EXISTS $this->dbname",false);
}
try{
$this->conexion = new PDO('mysql:hostname='.$this->hostname.';dbname='.$this->dbname,$this->usuario,$this->password);
}catch(PDOException $e){
echo $e->getMessage();
die();
}
$this->execStatementSetter("CREATE TABLE IF NOT EXISTS users(id INT AUTO_INCREMENT,user varchar(50),pass varchar(250),PRIMARY KEY (id))",false);
}
}
?>