对不起,我是新手使用pdo并试图关注http://php.net/manual/en/pdo.connections.php,我完全迷失了。我知道sql语句被删除了,我想切换到pdo,我无法弄明白。
代码:
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];
}
错误:
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
我很遗憾出了什么问题。 getKey()只在我这个1 php文件中声明。
我做错了什么?
好的,所以我在这里跟踪了一个指南http://jayblanchard.net/demystifying_php_pdo.html,并且我被困在一个新的部分大声笑。
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);
}
和输出:
Array ( [0] => Array ( [key] => aitjjdjjdnvjdvnj12235435sdfj324jj23j24nj789 ) )
如何只显示密钥的值?!?
弄清楚了lol抱歉的家伙
print_r($results[0]['key']);