<tfoot id='Zifqf'></tfoot>

      <small id='Zifqf'></small><noframes id='Zifqf'>

    1. <i id='Zifqf'><tr id='Zifqf'><dt id='Zifqf'><q id='Zifqf'><span id='Zifqf'><b id='Zifqf'><form id='Zifqf'><ins id='Zifqf'></ins><ul id='Zifqf'></ul><sub id='Zifqf'></sub></form><legend id='Zifqf'></legend><bdo id='Zifqf'><pre id='Zifqf'><center id='Zifqf'></center></pre></bdo></b><th id='Zifqf'></th></span></q></dt></tr></i><div id='Zifqf'><tfoot id='Zifqf'></tfoot><dl id='Zifqf'><fieldset id='Zifqf'></fieldset></dl></div>
      • <bdo id='Zifqf'></bdo><ul id='Zifqf'></ul>
    2. <legend id='Zifqf'><style id='Zifqf'><dir id='Zifqf'><q id='Zifqf'></q></dir></style></legend>

    3. PHP 数据库 PDO 连接

      时间:2023-10-05

      <legend id='oBzCb'><style id='oBzCb'><dir id='oBzCb'><q id='oBzCb'></q></dir></style></legend>

          <tbody id='oBzCb'></tbody>

          <bdo id='oBzCb'></bdo><ul id='oBzCb'></ul>
          <i id='oBzCb'><tr id='oBzCb'><dt id='oBzCb'><q id='oBzCb'><span id='oBzCb'><b id='oBzCb'><form id='oBzCb'><ins id='oBzCb'></ins><ul id='oBzCb'></ul><sub id='oBzCb'></sub></form><legend id='oBzCb'></legend><bdo id='oBzCb'><pre id='oBzCb'><center id='oBzCb'></center></pre></bdo></b><th id='oBzCb'></th></span></q></dt></tr></i><div id='oBzCb'><tfoot id='oBzCb'></tfoot><dl id='oBzCb'><fieldset id='oBzCb'></fieldset></dl></div>
            • <tfoot id='oBzCb'></tfoot>

              <small id='oBzCb'></small><noframes id='oBzCb'>

              1. 本文介绍了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="',$badge['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 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:MySQL 从带有 PDO 的 SELECT 插入 下一篇:未捕获的异常 'PDOException' 带有消息 'S

                相关文章

                最新文章

                <small id='hZAi8'></small><noframes id='hZAi8'>

                <tfoot id='hZAi8'></tfoot>
                1. <legend id='hZAi8'><style id='hZAi8'><dir id='hZAi8'><q id='hZAi8'></q></dir></style></legend>
                  • <bdo id='hZAi8'></bdo><ul id='hZAi8'></ul>
                    <i id='hZAi8'><tr id='hZAi8'><dt id='hZAi8'><q id='hZAi8'><span id='hZAi8'><b id='hZAi8'><form id='hZAi8'><ins id='hZAi8'></ins><ul id='hZAi8'></ul><sub id='hZAi8'></sub></form><legend id='hZAi8'></legend><bdo id='hZAi8'><pre id='hZAi8'><center id='hZAi8'></center></pre></bdo></b><th id='hZAi8'></th></span></q></dt></tr></i><div id='hZAi8'><tfoot id='hZAi8'></tfoot><dl id='hZAi8'><fieldset id='hZAi8'></fieldset></dl></div>