dongpinyao2203 2012-11-11 12:00
浏览 48
已采纳

在课堂上使用PDO和php和mysql

i am trying to use PDO in php using classes.

so i have defined one config file like that

<?php
global $configVars;

    $configVars['online'] = false;  

    if(($_SERVER['SERVER_NAME'])!='localhost' and ($_SERVER['SERVER_NAME'])!='abc')
    {
$configVars['dbhost']       = "localhost";      
$configVars['dbuser']       = "dbuser";   
$configVars['dbpassword']   = "dbpass";     
$configVars['dbname']       = "dbname"; 

}
?>

then i have defined class to connect the database

<?php
class dbClass{
var $dbHost,
      $dbUser,
        $dbName,
          $dbPass,
            $dbTable,
              $dbLink,
                $resResult,
                  $dbh,
                  $dbPort;  


        function dbClass(){
            global $configVars;
            $this->dbHost = $configVars['dbhost'];
            $this->dbUser = $configVars['dbuser'];
            $this->dbPass = $configVars['dbpassword'];
            $this->dbName = $configVars['dbname'];
            $this->connect_PDO();
        }

        function connect_PDO(){
            try {
            $dbh = new PDO('mysql:host='.$this->dbHost.';dbname='.$this->dbName.'', $this->dbUser, $this->dbPass);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo "Connected.";
            }
            catch(PDOException $e) {
            echo "I'm sorry Charlie, I'm afraid i cant do that.";
            file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
            $dbh = null;
            }
        }
 }       

?>

now i have defined another class to select the records from table

<?
class cms extends dbClass
{

function get_rec($id=0)
    {
        ($id==0?$addQuery="":$addQuery=" where id =".$id);
        $statement = $dbh->prepare("select * from TABLENAME :name order by id");
        $statement->execute(array(':name' => $addQuery));
        $row = $statement->fetchAll(); 
        return $row ;
    }   


}
?>

now when i am using this function in my PHP file like this

<?php 
$objCMS=new cms();
$getCMS=$objCMS->get_rec(1);
echo $getCMS[0];
?>

i got following out put

Connected.
Fatal error: Call to a member function prepare() on a non-object in /Applications/XAMPP/xamppfiles/htdocs

i am trying to sort this out from last 2 days but no success.

i am new to PDO and want to use it.

Thanks

  • 写回答

3条回答 默认 最新

  • 普通网友 2012-11-11 12:03
    关注

    You need to call dbClass() in your constructor for cms

    add this to the cms class

    function cms() {
        parent::dbClass();
    }
    

    and change get_rec to have:

    $statement = $this->dbh->prepare("select * from TABLENAME :name order by id");
    

    when using $dbh you need to reference it as $this->dbh

    You need to make the $dbh to $this->dbh changes in you need to update connect_PDO() also

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。