变量不打印预期值?

时间:2015-10-03 04:36:01

标签: php

这是我的代码。数据库和表是成功创建的,但问题是当我尝试回显调试消息时,其中包含运行方法和查询时获取的状态和错误消息。当我试图回应它的打印00.不确定,但我认为问题可能是使用static(但我想要一个静态的方式),我自己尝试不同的代码和搜索后的代码在线使用不同的关键字,发现很多,但没有多大帮助。现在,事情是我甚至不知道要搜索什么,我想知道我是否用错误的关键字搜索?所以我想在这里发帖。

<?php
//creates required databases & tables

class Database
{
    public static $debug_message="";
    //root
    private static $host="localhost";
    private static $root="root";
    private static $root_pass="";
    //c db
    private static $user="c_user";
    private static $pass="c_pass";
    private static $db="c";
    //tables
    static $student="student";
    static $lecturer="lecturer";
    //field sizes
    const SMALL_TEXT=25;
    const MEDIUM_TEXT=50;
    const BIG_TEXT=100;
    const LARGE_TEXT=225;


    public static function CreateDatabase()
    {
        self::$debug_message+="CREATING DATABASE".self::$db." with user ".self::$user." password ".self::$pass;
        try{
            $dbh=new PDO("mysql:host=".self::$host,self::$root,self::$root_pass);
            $dbh->exec("CREATE DATABASE `".self::$db."`;
                        CREATE USER '".self::$user."'@'localhost' IDENTIFIED BY '".self::$pass."';
                        GRANT ALL ON `".self::$db."`.* TO '".self::$user."'@'localhost';
                        FLUSH PRIVILEGES;")
            or die(print_r($dbh->errorInfo(),true));
        }catch(PDOException $e){
            self::$debug_message+=$e->getMessage();
        }


    }
    public static function CreateTables()
    {
        self::CreateStudentTable();
        self::CreateLecturerTable();
        //echo for debugging
        echo self::$debug_message;
        return self::$debug_message;
    }
    static function CreateStudentTable()
    {
        self::$debug_message+="Creating Student Table <br/>";
        try {
             $dbh=new PDO("mysql:host=".self::$host.";dbname=".self::$db,self::$root,self::$root_pass);
             $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
             $sql ="CREATE table ".self::$student."(
             student_id INT( 11 ) AUTO_INCREMENT PRIMARY KEY,
             firstname VARCHAR(".self::MEDIUM_TEXT."),
             lastname VARCHAR(".self::MEDIUM_TEXT."),
             nationality  VARCHAR(".self::SMALL_TEXT."),
             enrollment_status  INT(1) DEFAULT 0 NOT NULL);" ;
             $dbh->exec($sql);
        }catch(PDOException $e){
            self::$debug_message+=$e->getMessage();

        }
    }
    static function CreateCourseTable()
    {
        self::$debug_message+="Creating Course Table <br/> ";
        try {
            $dbh=new PDO("mysql:host=".self::$host.";dbname=".self::$db,self::$user,self::$pass);
             $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
             $sql ="CREATE table ".self::$course."(
             course_id INT( 11 ) AUTO_INCREMENT PRIMARY KEY,
             course_name VARCHAR(".self::BIG_TEXT."),
             course_code VARCHAR(".self::SMALL_TEXT."),
             mqa_level INT(2));" ;
             $dbh->exec($sql);
        }catch(PDOException $e){
            self::$debug_message+=$e->getMessage();
        }
    }
    static function CreateLecturerTable()
    {
        self::$debug_message+="Creating Lecturer Table <br/>";
        try {
             $dbh=new PDO("mysql:host=".self::$host.";dbname=".self::$db,self::$user,self::$pass);
             $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
             $sql ="CREATE table ".self::$lecturer."(
             lecturer_id INT( 11 ) AUTO_INCREMENT PRIMARY KEY,
             first_name VARCHAR(".self::MEDIUM_TEXT."),
             last_name VARCHAR(".self::MEDIUM_TEXT."),
             nationality  VARCHAR(".self::SMALL_TEXT."),
             enrollment_status  INT(1) DEFAULT 0 NOT NULL);" ;
             $dbh->exec($sql);
        }catch(PDOException $e){
            self::$debug_message+=$e->getMessage();
        }
    }

}

//Tried two ways for debugging nothing works

//static way
Database::CreateDatabase();
echo Database::CreateTables();
//prints 00

//instance
$db=new Database();
$db->CreateDatabase();
echo $db->CreateTables();
//prints 00

结果

0000

但我希望它能打印出类似下面的内容

CREATING DATABASE c with user c_user  password c_pass
//if any error while creating database, that error message here
Creating Student Table
//if any error while creating student table, that error message here
Creating Lecturer Table
////if any error while creating lecturer table, that error message here

当我直接在内部使用字符串并从静态函数回显时,字符串会成功打印,但是当我将其分配给变量并尝试从实例的主函数回显时,它不起作用。

感谢任何帮助。在此先感谢:)

1 个答案:

答案 0 :(得分:2)

问题实际上与您对static的使用毫无关系。相反,问题是要在php中连接字符串,使用.而不是+

而不是

self::$debug_message+="Creating Student Table <br/>";

你应该

self::$debug_message.="Creating Student Table <br/>";

0的输出就是您try to add two strings together时发生的事情。