php - 使用代码连接数据库时的空白页面

时间:2016-05-29 09:10:10

标签: php mysqli

当使用代码连接到使用php的数据库时,我得到一个空白页面 在运行以下代码时,即使连接代码外侧的任何打印语句也不打印空白页

<?php

$link = new mysqli('localhost', 'root', '7610', 'sites');
if ($link) {
print "connected";
}
else {  print "faild";}

?>

2 个答案:

答案 0 :(得分:1)

尝试使用mysqli_connect()而不是mysqli()

答案 1 :(得分:0)

大多数人都认为PDO比mysqli函数提供了很多优势....为什么不试试这条路线呢?你很高兴你做到了......这就是基于PDO的版本会是什么样的......

    <?php

        //DATABASE CONNECTION CONFIGURATION:
        defined("HOST")     or define("HOST",   "localhost");           //REPLACE WITH YOUR DB-HOST
        defined("DBASE")    or define("DBASE",  "sites");               //REPLACE WITH YOUR DB NAME
        defined("USER")     or define("USER",   "root");                //REPLACE WITH YOUR DB-USER
        defined("PASS")     or define("PASS",   "7610");                //REPLACE WITH YOUR DB-PASS

        try {
            $dbh        = new PDO('mysql:host='.HOST.';dbname='. DBASE,USER,PASS);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        }catch(PDOException $e){
            echo $e->getMessage();
        }

        if($dbh instanceof PDO){
            print "The Coast is clear...";
        }else{
            print "We have a Huge Storm on our hands.... BAIL ;-)";
        }

        // FROM HERE ON, YOU COULD START USING $dbh AS YOUR PDO OBJECT...
        // FOR EXAMPLE YOU COULD DO A SELECT LIKE SO:           
        $sql        = 'SELECT u.* FROM user AS u';
        $stmt       = $dbh->prepare($sql);
        $stmt->execute();
        $result     = $stmt->fetchAll(PDO::FETCH_OBJ);
        var_dump($result);