如何正确使用PDO?

时间:2017-01-21 02:52:27

标签: php mysql pdo

快速编辑:虽然它说的是MAMP,但我只是将我的web项目存储在那里而我没有使用它提供的mysql服务器(虽然当我尝试它时抛出相同的错误),我使用MySQL workbench *附带的mysql

在我正在处理的PHP代码中,我按如下方式连接到我的数据库:

 *{
      margin: 0;
    }
    html {
      position: relative;
      min-height: 100%;
    }
    body {
      background: #232526;
      background: -webkit-linear-gradient(to right, #232526 ,    #414345);
      background: linear-gradient(to right, #232526 , #414345);
      margin: 0em;
      font-family: 'Titillium Web', sans-serif;
      
      font-size: 12px;
    }
    img{
      width: auto;
      height: 100%;
    }
    footer{
      position: fixed;
      margin: 0;
      padding: 0;
      bottom: 0;
      width: 100%;
      background-color: rgba(128,128,128,.3);
      text-align: center; /*centers all of cooter */
      max-height: 155px;
      border-top: 1px solid #232526;
    }
    .left, .right{
      display: inline-block;
      list-style-type: none;
      width: auto;
      font-size: 100%;
      vertical-align: top;
    }
    .left li{
    text-align: right; /*aligns right within center */
    color: rgb(192,192,192);
    text-decoration: none;
    }
    .right li{
      text-align: left;
    }
    footer ul.right li a{
      text-decoration: none;
      color: rgb(192,192,192);
    }
    .material-icons.md-14{
      font-size: 1.167em;
    }
    .sliding-middle-out {
      display: inline-block;
    }
    .sliding-middle-out:after {
      content: '';
      display: block;
      margin: auto;
      height: 0.167em;
      width: 0;
      background: transparent;
      transition: width .5s ease, background-color .5s ease;
    }
    .sliding-middle-out:hover:after {
      width: 100%;
    }
    ul.topnav {
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: rgba(128,128,128,.3);
      display: inline-block;
      text-align: center;
      width: 100%;
      position: fixed;
      border-bottom: 1px solid #232526;
    }
    ul.topnav li{
      display: inline-block;
      text-align: center;
    }
    ul.topnav li a {
      color: rgb(192,192,192);
      padding: 1.167em 1.333em;
      text-decoration: none;
    }
    ul.topnav li a:hover {background-color: #232526;}
    ul.topnav li a.active {background-color: rgba(228,230,229,.3);}
    ul.topnav li.icon {display: none;}
    a.navbar-brand{
      text-decoration: none;
      color: #666666;
    }
    .imgcont{
      height: auto;
      max-width: 100%;
      margin: 0;
      padding: 4.167em 0 0 0;
      text-align: center;
    }
    ul.imgcont li{
      display: inline-block;
      width: auto;
    }
    ul.imgcont li a:hover {
      background-color: rgba(200,200,200,.1);
      border: 0.083em solid rgba(0,0,0,.5);
      border-radius: 5px;
    }
    .text_box{
      display: inline-block;
      text-align: left;
      vertical-align: top;
      font-size: 28px;
      color: rgb(192,192,192);
      transform: translateY(150%);
    }
    @media screen and (max-width:760px) {
      ul.topnav li:not(:first-child) {display: none;} /* makes navbar disapearr */
      ul.topnav li.icon { /* creates the block for the toggle */
        float: right;
        display: inline-block;
      }
      ul.topnav.responsive {position: relative;}
      ul.topnav.responsive li.icon {
        position: absolute;
        right: 0;
        top: 0;
      }
      ul.topnav.responsive li {
        float: none;
        display: inline;
      }
      ul.topnav.responsive li a {
        display: block;
        text-align: left;
      }
    }
    @media screen and (max-width:1216px) { /* These are here to fix the overlap issue */
      ul.imgcont{
        margin: 0;
        padding: 4.167em 0 12.500em 0;
      }
    }
    @media screen and (max-width:1083px) {
      ul.imgcont{
        margin: 0;
        padding: 8.333em 0 12.500em 0;
      }
    }
    @media screen and (max-width:897px) {
      .text_box{
        transform: translateY(0%);
      }
    }
    @media screen and (max-width:760px) {
      ul.imgcont{
        margin: 0;
        padding: 4.167em 0 170px 0;
      }
    }
    @media screen and (max-width:755px) {
      ul.imgcont{
        margin: 0;
        padding: 4.167em 0 12.500em 0;
      }
      .hidden-xs {
        display: none !important;
      }
    }
    #home.sliding-middle-out:hover:after {
      background-color:grey;
    }
    #homer.sliding-middle-out:hover:after {
      background-color:black;
    }
    #warrior.sliding-middle-out:hover:after {
      background-color:#C79C6E;
    }
    #druid.sliding-middle-out:hover:after {
      background-color:#FF7D0A;
    }
    #priest.sliding-middle-out:hover:after {
      background-color:#FFFFFF;
    }
    #paladin.sliding-middle-out:hover:after {
      background-color:#F58CBA;
    }
    #hunter.sliding-middle-out:hover:after {
      background-color:darkgreen;
    }
    #mage.sliding-middle-out:hover:after {
      background-color:#69CCF0	;
    }
    #shaman.sliding-middle-out:hover:after {
      background-color:#0070DE;
    }
    #warlock.sliding-middle-out:hover:after {
      background-color:#9482C9;
    }
    #rogue.sliding-middle-out:hover:after {
      background-color:#FFF569;
    }
    #minions.sliding-middle-out:hover:after {
      background-color:#C41F3B;
    }
    #spells.sliding-middle-out:hover:after {
      background-color:#A330C9;
    }
    #home.sliding-middle-out:hover:after {
      background-color:black;
    }
    #warrior.sliding-middle-out:hover:after {
      background-color:#C79C6E;
    }
    #druid.sliding-middle-out:hover:after {
      background-color:#FF7D0A;
    }
    #priest.sliding-middle-out:hover:after {
      background-color:#FFFFFF;
    }
    #paladin.sliding-middle-out:hover:after {
      background-color:#F58CBA;
    }
    #hunter.sliding-middle-out:hover:after {
      background-color:#ABD473
    }
    #mage.sliding-middle-out:hover:after {
      background-color:#69CCF0	;
    }
    #shaman.sliding-middle-out:hover:after {
      background-color:#0070DE;
    }
    #warlock.sliding-middle-out:hover:after {
      background-color:#9482C9;
    }
    #rogue.sliding-middle-out:hover:after {
      background-color:#FFF569;
    }
    #minions.sliding-middle-out:hover:after {
      background-color:#C41F3B;
    }
    #spells.sliding-middle-out:hover:after {
      background-color:#A330C9;
    }
    #footer_MSoG.sliding-middle-out:hover:after {
      background-color:black;
    }
    #footer_ONiK.sliding-middle-out:hover:after {
      background-color:black;
    }
    #footer_WotOG.sliding-middle-out:hover:after {
      background-color:black;
    }
    #footer_TLoE.sliding-middle-out:hover:after {
      background-color:black;
    }
    #footer_TGT.sliding-middle-out:hover:after {
      background-color:black;
    }
    #footer_BrM.sliding-middle-out:hover:after {
      background-color:black;
    }
    
    
    

现在,我通过我的终端(连接成功)测试了数据库的用户和密码,以及我的IDE(phpStorm)提供的也成功连接的接口。

我的问题如下:如何解决以下错误:

致命错误:未捕获错误:在/Applications/MAMP/htdocs/GIS/4Musketeers/PHP/common.php:20中调用null上的成员函数query()

现在我知道它提到第20行,它引用了我的foreach块的开始:

 
    
      " href="https://fonts.googleapis.com/icon?family=Material+Icons">
      <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web">
      <link rel="stylesheet" type="text/css" href="../../../Cardspoiler.css">
    
   
        <ul class="topnav" id="myTopnav">
          <li><a id="homer" class="sliding-middle-out" href="../../Navbar/Home.html">Cardspoiler</a></li>
          <li><a id="home" class="sliding-middle-out" href="Home.html"><i class="material-icons md-14">home</i>Home</a></li> <!-- using the googleapis.com stylesheet -->
          <li><a id="warrior" class="sliding-middle-out" href="Warrior.html"><i class="material-icons md-14">usb</i>Warrior</a></li>
          <li><a id="druid" class="sliding-middle-out" href="Druid.html"><i class="material-icons md-14">brightness_high</i>Druid</a></li>
          <li><a id="priest" class="sliding-middle-out" href="Priest.html"><i class="material-icons md-14">add_circle</i>Priest</a></li>
          <li><a id="paladin" class="sliding-middle-out" href="Paladin.html"><i class="material-icons md-14">star_half</i>Paladin</a></li>
          <li><a id="hunter" class="sliding-middle-out" href="Hunter.html"><i class="material-icons md-14">my_location</i>Hunter</a></li>
          <li><a id="mage" class="sliding-middle-out" href="Mage.html"><i class="material-icons md-14">whatshot</i>Mage</a></li>
          <li><a id="shaman" class="sliding-middle-out" href="Shaman.html"><i class="material-icons md-14">opacity</i>Shaman</a></li>
          <li><a id="warlock" class="sliding-middle-out" href="Warlock.html"><i class="material-icons md-14">person_outline</i>Warlock</a></li>
          <li><a id="rogue" class="sliding-middle-out active" href="Rogue.html"><i class="material-icons md-14">visibility_off</i>Rogue</a></li>
          <li><a id="minions" class="sliding-middle-out" href="Minions.html"><i class="material-icons md-14">keyboard_arrow_up</i>Minions</a></li>
          <li><a id="spells" class="sliding-middle-out" href="Spells.html"><i class="material-icons md-14">keyboard_arrow_down</i>Spells</a></li>
          <li class="icon">
            <a href="javascript:void(0);" style="font-size:30px;" onclick="myFunction()">☰</a>
          </li>
        </ul>
        <div class="imgcont">
          <img src="Counterfeit_Coin_I.png" alt="Counterfeit Coin"></a>
          <div class="text_box">
            Counterfeit Coin <br> Cost: 0 <br> Type: Spell <br> Text: Gain 1 Mana Crystal this turn only.
              </div>
        </div>
      <footer>
          <ul class="left hidden-xs">
            <li>CARDSPOILER.COM <br> Freedom to know <br> We are here to bring you visual, leaked spoilers for all of the <br> cards you love within a simple gallery setting.</li>
            <li><a href="mailto:help@cardspoiler.com" style="text-decoration:none;color:#9B764C;font-size: 10px;">CONTACT US</a>
              <a href="../../Privacy_Policy.html" style="text-decoration:none;color:#9B764C;font-size: 10px;">PRIVACY POLICY</a></li>
            </ul>
            <ul class="right">
              <li><a id="footer_MSoG" class="sliding-middle-out" href="MSoG.html">Mean Streets of Gadgetzan</a></li>
              <li><a id="footer_ONiK" class="sliding-middle-out" href="ONiK.html">One Night in Karazhan</a></li>
              <li><a id="footer_WotOG" class="sliding-middle-out" href="WotOG.html">Whispers of the Old Gods</a></li>
              <li><a id="footer_TLoE" class="sliding-middle-out" href="tLoE.html">The League of Explorers</a></li>
              <li><a id="footer_TGT" class="sliding-middle-out" href="TGT.html">The Grand Tournament</a></li>
              <li><a id="footer_BrM" class="sliding-middle-out" href="BrM.html">Blackrock Mountain</a></li>
            </ul>
        </footer>
        <script src="../../../Cardspoiler.js"></script>
    
      
   

我非常确信查询是正确的,因为它确实为我提供了我感兴趣的结果。查询是:

$dbh = new PDO('mysql:host=localhost:3306;dbname=mydb', $user, $pass);

结果是:

function printDirectors($sqlQuery) {
    $index = 0; //Counting index for our table
    global $dbh; //this is how we refer to our global $dbh up top.

    foreach ($dbh->query($sqlQuery) as $result) {
        echo "<tr><td class=\"index\">";
        echo $index + 1 ."</td>";
        echo "<td class=\"FirstName\">";
        echo $result['first_name'] ."</td>";
        echo "<td class=\"LastName\">";
        echo $result['last_name'];
        echo "</td></tr>";
        $index++;
    }
}

这是我第一次真正使用PDO或SQL与PHP,所以任何建议都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

正如输出所述,你得到这个的原因是因为你从未实例化$dbh。 PHP尝试在其上调用query方法,但它为空。