Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
A basic pdo database class
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="griimnak" data-source="post: 354966" data-attributes="member: 35695"><p>As you may know, mysql is depreciated in the latest php release. </p><p>You could use mysqli but i prefer pdo. this is a database class that I use on my personal site:</p><p></p><p><span style="font-size: 15px">database.php:</span></p><p><span style="font-size: 15px">[PHP]class Database {</span></p><p><span style="font-size: 15px"> </span></p><p><span style="font-size: 15px"> public static function dbConnect() {</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"> global $config;</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"> try {</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"> $username = $config['db']['user'];</span></p><p><span style="font-size: 15px"> $password = $config['db']['pass'];</span></p><p><span style="font-size: 15px"> $dbb = $config['db']['database'];</span></p><p><span style="font-size: 15px"> $hostname = $config['db']['host'];</span></p><p><span style="font-size: 15px"> $conn = new pdo("mysql:host=$hostname;dbname=$dbb;", $username, $password);</span></p><p><span style="font-size: 15px"> $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);</span></p><p><span style="font-size: 15px"> return $conn;</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"> } catch(PDOException $e) {</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"> $dberror = '<b style="text-color: red;"> Output a custom error with style </b>';</span></p><p><span style="font-size: 15px"> die($dberror);</span></p><p><span style="font-size: 15px"> </span></p><p><span style="font-size: 15px"> }</span></p><p><span style="font-size: 15px"> }</span></p><p><span style="font-size: 15px">}[/PHP]</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">config.php:</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">[PHP]</span></p><p><span style="font-size: 15px">$config['db']['host'] = 'localhost';</span></p><p><span style="font-size: 15px">$config['db']['user'] = 'root';</span></p><p><span style="font-size: 15px">$config['db']['pass'] = 'pass';</span></p><p><span style="font-size: 15px">$config['db']['database'] = 'db';</span></p><p><span style="font-size: 15px"> [/PHP]</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">finally, the usage</span></p><p><span style="font-size: 15px">index.php:</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">[PHP]</span></p><p><span style="font-size: 15px">require_once 'config.php';</span></p><p><span style="font-size: 15px">require_once 'database.php';</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">$query = Database::dbConnect()->prepare("SELECT * FROM sometable");</span></p><p><span style="font-size: 15px">$query->execute();</span></p><p><span style="font-size: 15px">$result = $query->fetchAll();</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">foreach($result as $row) {</span></p><p><span style="font-size: 15px"> </span></p><p><span style="font-size: 15px"> $certain_row = $row['username'];</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">}</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">if($certain_row == 'griimnak'){</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px"> echo 'hi griimnak';</span></p><p><span style="font-size: 15px"></span></p><p><span style="font-size: 15px">}[/PHP]</span></p><p><span style="font-size: 15px"></span></p><p>it's simple and gets the job done. Keep in mind php isn't my best language, but this is good base for you to build upon.</p></blockquote><p></p>
[QUOTE="griimnak, post: 354966, member: 35695"] As you may know, mysql is depreciated in the latest php release. You could use mysqli but i prefer pdo. this is a database class that I use on my personal site: [I][SIZE=3][/SIZE][/I] [SIZE=4]database.php: [PHP]class Database { public static function dbConnect() { global $config; try { $username = $config['db']['user']; $password = $config['db']['pass']; $dbb = $config['db']['database']; $hostname = $config['db']['host']; $conn = new pdo("mysql:host=$hostname;dbname=$dbb;", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $conn; } catch(PDOException $e) { $dberror = '<b style="text-color: red;"> Output a custom error with style </b>'; die($dberror); } } }[/PHP] config.php: [PHP] $config['db']['host'] = 'localhost'; $config['db']['user'] = 'root'; $config['db']['pass'] = 'pass'; $config['db']['database'] = 'db'; [/PHP] finally, the usage index.php: [PHP] require_once 'config.php'; require_once 'database.php'; $query = Database::dbConnect()->prepare("SELECT * FROM sometable"); $query->execute(); $result = $query->fetchAll(); foreach($result as $row) { $certain_row = $row['username']; } if($certain_row == 'griimnak'){ echo 'hi griimnak'; }[/PHP] [/SIZE] it's simple and gets the job done. Keep in mind php isn't my best language, but this is good base for you to build upon. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
A basic pdo database class
Top