PHP Databases PDO connections(PHP 数据库 PDO 连接)
问题描述
嘿伙计们,我在使用 php 中的 PDO 时遇到了一些麻烦,因为它返回的错误是未定义的索引.函数和查询返回结果的代码是这样的:
Hey guys im having a little trouble with the PDO in php as the error it is returning is an undefined index. The code for the function and query and return of result is this:
function getUserDetails($user) {
$db = connect();
try {
$stmt = $db->prepare('SELECT name,addr AS address,team
FROM TreasureHunt.Player LEFT OUTER JOIN TreasureHunt.MemberOf ON (name=player)
LEFT OUTER JOIN TreasureHunt.PlayerStats USING (player)
WHERE name=:user');
$stmt->bindValue(':user', $user, PDO::PARAM_STR);
$stmt->execute();
$results = $stmt->fetchAll();
$stmt->closeCursor();
} catch (PDOException $e) {
print "Error : " . $e->getMessage();
die();
}
return $results;
}
但是,当运行索引页面的代码时,我收到一条错误消息:注意:未定义索引:名称
However when running the code for the index page i get an error that says Notice: Undefined index: name
索引代码如下:
try {
$details = getUserDetails($_SESSION['player']);
echo '<h2>Name</h2> ',$details['name'];
echo '<h2>Address</h2>',$details['address'];
echo '<h2>Current team</h2>',$details['team'];
echo '<h2>Hunts played</h2> ',$details['nhunts'];
echo '<h2>Badges</h2>';
foreach($details['badges'] as $badge) {
echo '<span class="badge" title="JywkYmFkZ2Vb"desc'],'">',$badge['name'],'</span><br />';
}
} catch (Exception $e) {
echo 'Cannot get user details';
}
我的问题是为什么它会发出通知,我该如何解决这个问题?
my question is why is it throwing a notice and how do i go around this problem?
推荐答案
fetchAll
返回多维数组中的所有结果(可能是多行)>:
fetchAll
returns all results (potentially multiple rows) in a multidimensional array:
array(
0 => array(/* first row */),
1 => array(/* second row */),
...
)
这就是为什么数组没有直接索引'name'
,它需要是[0]['name']
.
或者你不应该fetchAll
,而是fetch
.
That's why the array doesn't have a direct index 'name'
, it needs to be [0]['name']
.
Or you shouldn't fetchAll
, just fetch
.
这篇关于PHP 数据库 PDO 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 数据库 PDO 连接


- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01