php - variable doesn't print the expected value? -
here code. database , tables created successfully, thing when try echo debug messages, way contains status , error messages acquired while running methods , queries. when try echo printing 00. not sure, think problem may using static
(but want static way) , tried code in in different ways on own , after searching online different keywords, found lot didn't much.now, thing don't know search for, wonder searching wrong keywords? thought of posting here.
<?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." 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 '".self::$pass."'; grant on `".self::$db."`.* '".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 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 2 ways debugging nothing works //static way database::createdatabase(); echo database::createtables(); //prints 00 //instance $db=new database(); $db->createdatabase(); echo $db->createtables(); //prints 00
result
0000
but expect print following
creating database c user c_user password c_pass //if error while creating database, error message here creating student table //if error while creating student table, error message here creating lecturer table ////if error while creating lecturer table, error message here
when directly use strings inside , echo static functions strings prints when assign variable , tries echo main function of instance doesn't work.
any appreciated. in advance :)
the problem has nothing use of static
@ all. rather, problem concatenate strings in php, use .
, not +
.
instead of
self::$debug_message+="creating student table <br/>";
you should have
self::$debug_message.="creating student table <br/>";
an output of 0
happens when try add 2 strings together.
Comments
Post a Comment