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] Basics of 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="brsy" data-source="post: 322908" data-attributes="member: 2912"><p>Since the deprecation of MySQL, many people are confused whether they should use MySQLi or PDO. They are both relatively the same, but it's all a matter of preference. I prefer to use PDO, so I'm going to demonstrate how to utilize a few of the basic functions within PDO.</p><p></p><p>Firstly, like every database interface, you need to connect to the database first.</p><p>[SPOILER="Connecting to the Database"]</p><p>[PHP]$zip['PDO']['Hostname'] = 'localhost'; #host name: usually localhost</p><p>$zip['PDO']['Username'] = 'root'; #username: usually root</p><p>$zip['PDO']['Password'] = 'abc123'; #password: usually null or root</p><p>$zip['PDO']['Database'] = 'zip'; # name of the database</p><p></p><p>try {</p><p> $pdo = new PDO('mysql:host='. $zip['PDO']['Hostname'] .';dbname=' . $zip['PDO']['Database'], $zip['PDO']['Username'], $zip['PDO']['Password']);</p><p> $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);</p><p>} catch(PDOException $e) {</p><p> die('ERROR: ' . $e->getMessage());</p><p>}[/PHP]</p><p>[/SPOILER]</p><p></p><p>The above code will "try" to connect to the database, and if it is unsuccessful, it will return an error message explaining exactly what the problem is. Please be sure that error reporting is enabled, which can simply be done by adding <em>error_reporting(E_ALL); </em>to the top of your script. Next I will show you how to run a query using the wonderful prepare() and execute() function in PDO.</p><p>[SPOILER="Running a Query"]</p><p>[PHP]$sql = "SELECT * FROM users WHERE username = :username";</p><p> $stmt = $pdo->prepare($sql);</p><p> $stmt->execute(array(":username" => $username));[/PHP]</p><p>[/SPOILER]</p><p></p><p>As you can see above, instead of adding the variable directly into the SQL query, you put in something called a <em>named parameter</em>. You then define the values for each parameter by supplying it into an array() within the execute function. This also helps you prevent SQL injection, making your life a whole lot easier. The function names are also a bit self-explanatory, which makes it even easier to maneuver and utilize. Next I will show you how to return data after you've run your query, which is also fairly simple.</p><p></p><p>[SPOILER="Fetching Data After a Query"]</p><p>[PHP]$row = $stmt->fetch(PDO::FETCH_ASSOC); # fetches the results, and places them into the associative array $row.</p><p></p><p>echo $row['password']; # this would then echo the value of the column 'password'[/PHP]</p><p>[/SPOILER]</p><p></p><p>I hope you enjoyed this tutorial, as I wished that I had a tutorial showing me how to use PDO. I had to research each function which was a bit more time consuming. For more information on PDO, check out the docs on <a href="http://php.net/manual/en/book.pdo.php" target="_blank">PHP.net</a></p></blockquote><p></p>
[QUOTE="brsy, post: 322908, member: 2912"] Since the deprecation of MySQL, many people are confused whether they should use MySQLi or PDO. They are both relatively the same, but it's all a matter of preference. I prefer to use PDO, so I'm going to demonstrate how to utilize a few of the basic functions within PDO. Firstly, like every database interface, you need to connect to the database first. [SPOILER="Connecting to the Database"] [PHP]$zip['PDO']['Hostname'] = 'localhost'; #host name: usually localhost $zip['PDO']['Username'] = 'root'; #username: usually root $zip['PDO']['Password'] = 'abc123'; #password: usually null or root $zip['PDO']['Database'] = 'zip'; # name of the database try { $pdo = new PDO('mysql:host='. $zip['PDO']['Hostname'] .';dbname=' . $zip['PDO']['Database'], $zip['PDO']['Username'], $zip['PDO']['Password']); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { die('ERROR: ' . $e->getMessage()); }[/PHP] [/SPOILER] The above code will "try" to connect to the database, and if it is unsuccessful, it will return an error message explaining exactly what the problem is. Please be sure that error reporting is enabled, which can simply be done by adding [I]error_reporting(E_ALL); [/I]to the top of your script. Next I will show you how to run a query using the wonderful prepare() and execute() function in PDO. [SPOILER="Running a Query"] [PHP]$sql = "SELECT * FROM users WHERE username = :username"; $stmt = $pdo->prepare($sql); $stmt->execute(array(":username" => $username));[/PHP] [/SPOILER] As you can see above, instead of adding the variable directly into the SQL query, you put in something called a [I]named parameter[/I]. You then define the values for each parameter by supplying it into an array() within the execute function. This also helps you prevent SQL injection, making your life a whole lot easier. The function names are also a bit self-explanatory, which makes it even easier to maneuver and utilize. Next I will show you how to return data after you've run your query, which is also fairly simple. [SPOILER="Fetching Data After a Query"] [PHP]$row = $stmt->fetch(PDO::FETCH_ASSOC); # fetches the results, and places them into the associative array $row. echo $row['password']; # this would then echo the value of the column 'password'[/PHP] [/SPOILER] I hope you enjoyed this tutorial, as I wished that I had a tutorial showing me how to use PDO. I had to research each function which was a bit more time consuming. For more information on PDO, check out the docs on [URL='http://php.net/manual/en/book.pdo.php']PHP.net[/URL] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
[PHP] Basics of PDO
Top