dongzhangji4824 2014-03-07 11:09
浏览 42
已采纳

将mysql转换为PDO时的空白网页

Hi I have started to change my mysql to PDO before my code starts getting large and risk of causing problems i have tried changing a section of my code but the screen shows as white this is my original code:

class SelectList
{
    protected $conn;

        public function __construct()
        {
            $this->DbConnect();
        }

        protected function DbConnect()
        {
            include "db_config.php";
            $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
            mysql_select_db($db,$this->conn) OR die("can not select the database $db");
            return TRUE;
        }
         public function ShowPrinciple()
        {
            $sql = "SELECT principle.principle_id,principle.description,principle.section_id,COUNT(media.principle_id) as media_count
                    FROM principle 
                    LEFT OUTER JOIN media ON principle.principle_id = media.principle_id 
                    AND principle.section_id = media.section_id 
                    WHERE principle.section_id={$_POST['id']}
                    GROUP BY principle.principle_id,principle.description";
            $res = mysql_query($sql,$this->conn);
            $principle = '<option value="%">choose...</option>';
            while($row = mysql_fetch_array($res))
            {
                $principle .= '<option value="' . $row['principle_id'] . '">' . $row['description'].  '...('.$row['media_count'].') </option>';
            }
            return $principle;
        }
}
$opt = new SelectList();

I have tried a simple query to start off with to change but I seem to have done something wrong could some one point out to me I'm positive it will be something I have changed.

This is the code that I have currently got:

class SelectList
{

$host = '127.00.00.00';
$user = 'user';
$password = 'password';
$db =  'database'; 

 try{ 
    $conn = new PDO("mysql:host=$host;dbname=$db", $user, $password);
    echo 'connected to the database<br />';

    public function ShowPrinciple()
    {

        $stmt = $conn -> prepare(
                    "SELECT principle.principle_id,principle.description,principle.section_id,COUNT(media.principle_id) as media_count
                    FROM principle 
                    LEFT OUTER JOIN media ON principle.principle_id = media.principle_id 
                    AND principle.section_id = media.section_id 
                    WHERE principle.section_id={$_POST['id']}
                    GROUP BY principle.principle_id,principle.description");
        $q = $conn->query($stmt) or die("failed!)";
        $principle = '<option value="%">choose...</option>';
        while($row = fetchAll($q))
        {
            $principle .= '<option value="' . $row['principle_id'] . '">' . $row['description'].  '...('.$row['media_count'].') </option>';
        }
        return $principle;
    }catch(PDOException $e){
        echo $e->getMessage();
    }
}
$opt = new SelectList();

Any help would be much appreciated.

  • 写回答

3条回答 默认 最新

  • doukengzi3517 2014-03-07 15:39
    关注

    So you don 't know how to write classes, right? First: check your error reporting! Turn it up if it 's not turned up. PHP should output some fatal errors here. The following code can def not work:

    class Bla {
        $var1 = 'foo';
        $var2 = 'bar';
    
        try {
            public function doSth() { ... }
        } catch (SomethingException $e) {
            doSthWthExcpetion();
        }
    }
    

    That 's not proper PHP. Make yourself comfortable with dependency injection or something else, that work 's as this:

    class Bla {
        protected $dbHandle = null;
    
        public function __construct() {
            $this->dbConnect();
        }
    
        public function dbConnect() {
            $this->dbHandle = new PDO(...);
        }
    
        public function doSthDatabseish($variable) {
            try {
                $stmt = $this->dbHandle->prepare("SLECET something FROM myTable WHERE variable = :variable");
                $stmt->bindValue(':variable', $variable);
                $stmt->execute();
    
                foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
                    $value[] = $row;
                }
            } catch (PDOException $e) {
                // errorhandling
            }
    
            return $value;
        }
    }
    

    Do you see the difference? Never write code between functions. Code belongs in the function definition. As your first example class shows, you establish the database connection in a connectDB() function. So why don 't you in the PDO Version of your class?

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

报告相同问题?

悬赏问题

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