duanniedang3946 2016-07-12 15:34
浏览 201

PDO抛出错误未定义的变量:pdo in

I have 3 files: dbconnect (here's declaration of $pdo), core.php (file with class to manage) and test.php. I want to receive data from DB, but I have error: Notice: Undefined variable: pdo in C:\xampp\htdocs\project\core.php on line 24

In dbconnect $pdo is in try catch, but before this I put: $pdo=null(to make variable accessible) but it doesn't work. dbconnect ---> core.php(error here) ---> test.php;

 //dbconnect.php
<?php
$mysql_host = 'localhost';
$username = 'root';
$password = '';
$database = 'db'; 

$pdo = null;
try {
    $pdo = new PDO('mysql:host='.$mysql_host.';dbname='.$database.';charset=utf8', $username, $password );
}
catch(PDOException $e) {
    echo 'Połączenie nie mogło zostać utworzone.<br />';
}

?>

   //core.php
require_once('cms/dbconnect.php');
class getCore{
    public $link_allegro;
    public $link_facebook;


    function getLinks(){
$query= $pdo->query('SELECT `url` FROM `links` WHERE `title` = "facebook"');
$row = $query->fetch();
$this->link_facebook = $row["url"];

$query= $pdo->query('SELECT url FROM links WHERE title = "allegro"');
$row = $query->fetch();
$this->link_allegro = $row["url"];

$query->closeCursor();


    }
}

//test.php

<?php 
require_once('core.php');
$tmp = new getCore;
$tmp->getLinks();

echo $tmp->link_allegro;
echo $tmp->link_facebook;





?>

Anyone can solve this? Thanks.

  • 写回答

2条回答 默认 最新

  • dongsi2317 2016-07-12 15:53
    关注

    This is a scoping problem. $pdo doesn't exists in your getCore class. You can make a DatabaseConnect class to manage your db access.

    You can use this basic class for your database connection :

    <?php  
        class DatabaseConnect {
    
             private $mysql_host = 'localhost';
             private $username = 'root';
             private $password = '';
             private $database = 'db'; 
    
             private $pdo = null;
    
             public function getPdo(){
                   return $this->pdo;
             }
    
             public function __construct(){
                  try {
                      $this->pdo = new PDO('mysql:host='.$mysql_host.';dbname='.$database.';charset=utf8', $username, $password );
                  }
                  catch(PDOException $e) {
                      echo 'Połączenie nie mogło zostać utworzone.<br />';
                  }
              }
    
         }
     ?>
    

    You can getting your PDO instance in an other class with calling DatabaseConnect object -> getPdo() : - Instannciate a new DatabaseConnect. - Get PDO instance with the methof of the class.

    Like that :

      $databaseConnect = new DatabaseConnect();
    
      $pdo = $databaseConnect->getPdo();
    

    You next code :

    //core.php
    require_once('cms/dbconnect.php');
    class getCore{
         public $link_allegro;
         public $link_facebook;
    
    
         function getLinks(){
               $databaseConnect = new DatabaseConnect();
               $pdo = $databaseConnect-getPdo();
               $query= $pdo->query('SELECT `url` FROM `links` WHERE `title` = "facebook"');
               $row = $query->fetch();
               $this->link_facebook = $row["url"];
    
               $query= $pdo->query('SELECT url FROM links WHERE title = "allegro"');
               $row = $query->fetch();
               $this->link_allegro = $row["url"];
    
               $query->closeCursor();
         }
     }
    
    //test.php
    
    <?php 
    require_once('core.php');
    $tmp = new getCore;
    $tmp->getLinks();
    
    echo $tmp->link_allegro;
    echo $tmp->link_facebook;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据