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
[PHP] How to use PDO
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="IntactDev" data-source="post: 233130" data-attributes="member: 24757"><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">Hello my fellow DevBesters!</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">Today I will share with you the most useful (imo) interface for accessing databases. Here is what PHP.net says about it.</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">So let's get started</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"><strong>Step 1:</strong></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">First, you need to define your database credentials into variables.</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">[PHP]$config['PDO']['Hostname'] = 'localhost'; # Database hostname</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">$config['PDO']['Username'] = 'root'; # Database Username</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">$config['PDO']['Password'] = 'password'; # Database Password</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">$config['PDO']['Database'] = 'db'; # Database name</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">$config['PDO']['Data']['Source']['Name'] = 'mysql:dbname=' . $config['PDO']['Database'] . ';host='. $config['PDO']['Hostname'] .';';[/PHP]</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"><strong>Step 2:</strong></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">Initiate your connect to the database, and check for errors.</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">[PHP]try {</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"> $db = new PDO($config['PDO']['Data']['Source']['Name'], $config['PDO']['Username'], $config['PDO']['Password']); # Connect to the database</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">} catch(PDOException $e) {</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"> die($e->getMessage()); # if there is an error, the script will end and return the error message.</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">}[/PHP]</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"><strong>Step 3:</strong></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">Do database functions (like queries!)</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">[PHP]$query = $db->prepare('SELECT first_name, last_name, email FROM users ORDER BY first_name ASC'); # Unlike MySQL, with PDO you have to prepare the query first.</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">$query->execute(); # Then, you simply execute the query.</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"># Let's say you wanted to fetch all the results, you would do something like this</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">$result = $query->fetchAll(); # Fetches all results from query.</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"># Then you could do the following...</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">echo $result['first_name']; # Returns result of the 'first_name' column for the FIRST result.[/PHP]</span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)"></span></span></span></p><p><span style="font-size: 15px"><span style="font-family: 'Georgia'"><span style="color: rgb(44, 62, 80)">I hope you enjoyed the tutorial. If I made any mistakes (and I don't think I did) just let me know and I'll gladly fix/improve them!</span></span></span></p></blockquote><p></p>
[QUOTE="IntactDev, post: 233130, member: 24757"] [SIZE=4][FONT=Georgia][COLOR=rgb(44, 62, 80)]Hello my fellow DevBesters! Today I will share with you the most useful (imo) interface for accessing databases. Here is what PHP.net says about it. [/COLOR] [COLOR=rgb(44, 62, 80)]So let's get started [B]Step 1:[/B] First, you need to define your database credentials into variables. [PHP]$config['PDO']['Hostname'] = 'localhost'; # Database hostname $config['PDO']['Username'] = 'root'; # Database Username $config['PDO']['Password'] = 'password'; # Database Password $config['PDO']['Database'] = 'db'; # Database name $config['PDO']['Data']['Source']['Name'] = 'mysql:dbname=' . $config['PDO']['Database'] . ';host='. $config['PDO']['Hostname'] .';';[/PHP] [B]Step 2:[/B] Initiate your connect to the database, and check for errors. [PHP]try { $db = new PDO($config['PDO']['Data']['Source']['Name'], $config['PDO']['Username'], $config['PDO']['Password']); # Connect to the database } catch(PDOException $e) { die($e->getMessage()); # if there is an error, the script will end and return the error message. }[/PHP] [B]Step 3:[/B] Do database functions (like queries!) [PHP]$query = $db->prepare('SELECT first_name, last_name, email FROM users ORDER BY first_name ASC'); # Unlike MySQL, with PDO you have to prepare the query first. $query->execute(); # Then, you simply execute the query. # Let's say you wanted to fetch all the results, you would do something like this $result = $query->fetchAll(); # Fetches all results from query. # Then you could do the following... echo $result['first_name']; # Returns result of the 'first_name' column for the FIRST result.[/PHP] I hope you enjoyed the tutorial. If I made any mistakes (and I don't think I did) just let me know and I'll gladly fix/improve them![/COLOR][/FONT][/SIZE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
[PHP] How to use PDO
Top