Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a554208

Browse files
Added PDO class I'm now using instead of class.MySQL.php
1 parent 41a505b commit a554208

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

‎class.PDO.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
class PDO {
4+
5+
public $pdo;
6+
private $error;
7+
8+
9+
function __construct() {
10+
$this->connect();
11+
}
12+
13+
function add_table_prefix($string){
14+
return DATABASE_PREFIX . $string;
15+
}
16+
17+
18+
function prep_query($query){
19+
return $this->pdo->prepare($query);
20+
}
21+
22+
23+
function connect(){
24+
if(!$this->pdo){
25+
26+
$dsn = 'mysql:dbname=' . DATABASE_NAME . ';host=' . DATABASE_HOST;
27+
$user = DATABASE_USER;
28+
$password = DATABASE_PASS;
29+
30+
try {
31+
$this->pdo = new PDO($dsn, $user, $password);
32+
return true;
33+
} catch (PDOException $e) {
34+
$this->error = $e->getMessage();
35+
die($this->error);
36+
return false;
37+
}
38+
}else{
39+
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
40+
return true;
41+
}
42+
}
43+
44+
45+
function table_exists($table_name){
46+
$stmt = $this->prep_query('SHOW TABLES LIKE ?');
47+
$stmt->execute(array($this->add_table_prefix($table_name)));
48+
return $stmt->rowCount() > 0;
49+
}
50+
51+
52+
function execute($query, $values = array()){
53+
$stmt = $this->pdo->prepare($query);
54+
$stmt->execute($values);
55+
return $stmt;
56+
}
57+
58+
function fetch($query, $values = array()){
59+
$stmt = $this->execute($query, $values);
60+
return $stmt->fetch(PDO::FETCH_ASSOC);
61+
}
62+
63+
function fetchAll($query, $values = array()){
64+
$stmt = $this->execute($query, $values);
65+
return $stmt->fetchAll(PDO::FETCH_ASSOC);
66+
}
67+
68+
function lastInsertId(){
69+
return $this->pdo->lastInsertId();
70+
}
71+
72+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /