dorv56831 2015-12-30 17:43
浏览 58
已采纳

pdo成员函数查询错误即将到来

I am getting Call to a member function query() on a non-object in G:\PleskVhosts\angelsoftweb.com\angelunitrax.angelsoftweb.com\header.php on line 116 

db.php

<?php
$servername = "########";
$username = "########";
$password = "########";
$dbh = "rfid";
try {
$conn = new PDO('mysql:host=localhost;dbname=$dbh', $username, 
$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
?>

header.php

<?php

foreach($sql=$conn->query("SELECT * FROM header_section") as $st) {
echo'
<div class="col-sm-2 col-md-2">
<ul style="margin-top: 13px;">
<li><a href="callus.php"><span style="margin-left:5px; color:white; 
font-size:16px; font-weight:bold;"><b>'.$st['call_us_name'].'</b></span>   
<span style="margin-left:5px; color:white; font-size:16px;
font-weight:bold;">'.$st['number'].'</span></a></li>
</ul>
</div>';
}

?>

I tried to solve this but not got what is the problem van some one help me with this to solve this as soon as possible...

  • 写回答

2条回答 默认 最新

  • dqalnwuci494308 2015-12-30 18:22
    关注

    The issue is variable interpolation in this line

    'mysql:host=localhost;dbname=$dbh'

    Replace single quote ' with double quote "

    Your script is taking variables in that line as string.

    That line should look like this

    "mysql:host=localhost;dbname=$dbh"

    Your db.php shoud look like this

    <?php
    $servername = "########";
    $username = "########";
    $password = "########";
    $dbh = "rfid";
    try {
    $conn = new PDO("mysql:host=localhost;dbname=$dbh", $username, 
    $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
    echo 'Error: ' . $e->getMessage();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?