0

I'm sorry I am new to using pdo and was trying to follow http://php.net/manual/en/pdo.connections.php and I am totally lost. I know sql statements are depricated and i want to switch to pdo and I cannot figure it out.

code:

function getKey() {
    $dbh = new PDO('mysql:host=localhost;dbname=ahs', $user, $pass, array(
PDO::ATTR_PERSISTENT => true

));

    $row = $dbh->query('SELECT `key` from `settings`');
    return $row['key'];
    $dbh = null;

    //$query = 'SELECT * FROM `'.$table_settings.'`';
    //$r_query = mysql_query($query);
    //$result = mysql_fetch_array($r_query);
    //return $result[$table_settings_key];
}

Error:

Fatal error: Cannot redeclare getKey() (previously declared in C:\xampp\htdocs\ahs\db\pdo\core.php:4) in C:\xampp\htdocs\ahs\db\pdo\core.php on line 12

I am so lost as to what is wrong. getKey() is only declared here in this 1 php file I have.

What am I doing wrong?

ok so I followed a guide here http://jayblanchard.net/demystifying_php_pdo.html and am stuck on a new part lol.

connect.php:

error_reporting(E_ALL);
ini_set('display_errors', 1);

define('USER', '');
define('PASS', '');

function dataQuery($query, $params) {
$queryType = explode(' ', $query);

// establish database connection
try {
    $dbh = new PDO('mysql:host=localhost;dbname=ahs', USER, PASS);
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
    echo $e->getMessage();
    $errorCode = $e->getCode();
}

// run query
try {
    $queryResults = $dbh->prepare($query);
    $queryResults->execute($params);
    if($queryResults != null && 'SELECT' == $queryType[0]) {
        $results = $queryResults->fetchAll(PDO::FETCH_ASSOC);
        return $results;
    }
    $queryResults = null; // first of the two steps to properly close
    $dbh = null; // second step to close the connection
}
catch(PDOException $e) {
    $errorMsg = $e->getMessage();
    echo $errorMsg;
}
}

core.php:

function getKey() {
$query = 'SELECT `key` FROM `settings`';
$params = array();
$results = dataQuery($query,$params);
print_r($results);
}

and the output:

Array ( [0] => Array ( [key] => aitjjdjjdnvjdvnj12235435sdfj324jj23j24nj789 ) ) 

how do I show just the value for the key?!?

figured it out lol sorry guys

print_r($results[0]['key']);
rackemup420
  • 1,600
  • 2
  • 15
  • 37

0 Answers0