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 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳