Created
September 14, 2012 17:23
-
-
Save PeeHaa/3723344 to your computer and use it in GitHub Desktop.
PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // always use full php tags | |
//require 'connect.inc.php'; | |
$dbConnection = new PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'pass'); | |
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
// mysql_* function?? Eeeeek | |
function query($query) | |
{ | |
$result = mysql_query($query) or dielogfile('MySQL foutmelding<br>'.$query.'<br>'.mysql_error()); | |
return $result; | |
} | |
// always initialize the variable you are going to use | |
$output = ''; | |
foreach($dbConnection->query('SELECT * FROM hbp_sharkies') as $row) { | |
$output .= '<h2><a href="/sharky/'.$row['url'].'/">'.$row['name'] . '</a></h2>'; | |
} | |
echo $output; | |
// inject it into the function | |
function getData(\PDO $dbConnection) | |
{ | |
// again always initialize your variables before using them | |
$data = ''; | |
foreach($dbConnection->query('SELECT * FROM hbp_sharkies') as $row) { | |
$data .= '<h2><a href="/sharky/'.$row['url'].'/">'.$row['name'] . '</a></h2>'; | |
} | |
return $data; | |
} | |
$output2 = getData($dbConnection); | |
echo $output2; | |
// no need to use the php close tag when the entire file is php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment