Skip to content

Instantly share code, notes, and snippets.

@PeeHaa
Created September 14, 2012 17:23
Show Gist options
  • Save PeeHaa/3723344 to your computer and use it in GitHub Desktop.
Save PeeHaa/3723344 to your computer and use it in GitHub Desktop.
PDO
<?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