How to call variables outside the class a. php page is a data link variable
$ Dbhost = "localhost ";
$ Dbname = "ffff ";
$ Dbuser = "cccc ";
$ Dbpwd = "123456 ";
$ Dbprefix = "user ";
$ Db_language = "gbk ";
B. The php page class contains the operation functions of various databases.
Require_once ("../data/a. php ");
Class mydatabase {
// Link to the database
Function opendata ($ database ){
How can I obtain all the variables outside the class in this method?
}
// Query a record
Function ReadOne ($ database ){
Self: opendata ($ database );
$ Rs = mysql_query ("SELECT * from fcc where id = 1 ");
$ Row = mysql_fetch_array ($ rs );
Self: closedata ();
Return $ row ["date"];
}
}
Reply to discussion (solution)
// Link to the database
Function opendata ($ database ){
Include ("../data/a. php ");
}
Or define it as a constant.
Require_once ("../data/a. php ")
Changes database-related parameters to global variables, and the variables with the same name may be overwritten.
You can also use the $ GLOBALS array to easily observe
You need to look at the basics.
Require_once (".. /data/. php "); class mydatabase {// link database function opendata ($ database) {// method 1 global $ dbhost, $ dbname, $ dbuser, $ dbpwd, $ dbprefix, $ db_language; // method 2 require_once (".. /data/. php "); // method 3 // In. php defines all variables as constants define ('Db _ host', 'test'); // DB_HOST echo DB_HOST ;} // query a record function ReadOne ($ database) {self: opendata ($ database); $ rs = mysql_query ("SELECT * from fcc where id = 1 "); $ row = mysql_fetch_array ($ rs); self: closedata (); return $ row ["date"] ;}}
I am very grateful to the above two experts. can I put the code for getting the variables in the constructor? because some variables will also be used in other methods, so it is not necessary to reference them again, but how does one return multiple variables of the constructor?
Class mydatabase {
Private $ myhost;
Private $ myuser;
Private $ mypwd;
Public function _ construct (){
Require_once ("../data/a. php ");
$ This-> myhost = $ dbhost;
$ This-> myuser = $ dbuser;
$ This-> mypwd = $ dbpwd;
Return $ this-> myhost;
How can I return multiple variables? if there is no return in the test, other methods cannot be obtained.
// Echo $ dbhost;
}
Function opendata ($ database ){
$ This->__ construct ();
$ Linkid = mysql_connect ($ this-> myhost, $ this-> myuser, $ this-> mypwd );
}
}
We recommend that the variables in a. php be written into the same array, or you can change them to function and return the array.
Naming pollution is sometimes disgusting.
Generally, it is best to change the configuration information to a constant.
I am very grateful to the above two experts. can I put the code for getting the variables in the constructor? because some variables will also be used in other methods, so it is not necessary to reference them again, but how does one return multiple variables of the constructor?
Class mydatabase {
Private $ myhost;
Private $ myuser;
Private $ mypwd;
Public function _ construct (){
Require_once ("../data/a. php ");
$ This-> myhost = $ dbhost;
$ This-> myuser = $ dbuser;
$ This-> mypwd = $ dbpwd;
Return $ this-> myhost;
How can I return multiple variables? if there is no return in the test, other methods cannot be obtained.
// Echo $ dbhost;
}
Function opendata ($ database ){
$ This->__ construct ();
$ Linkid = mysql_connect ($ this-> myhost, $ this-> myuser, $ this-> mypwd );
}
}
The first method is to use constants so that they can be used anywhere.
../Data/a. php
myhost = MYHOST; $this->myuser = MYUSER; $this->mypwd = MYPWD; } function opendata($database){ $linkid = mysql_connect($this->myhost, $this->myuser, $this->mypwd); }}?>
Second, use the config array
../Data/a. php
'localhost', 'myuser' => 'cccc', 'mypwd' => '123456');
dbConfig = require_once("../data/a.php"); } function opendata($database){ $this->__construct(); $linkid = mysql_connect($this->dbConfig['myhost'], $this->dbConfig['myuser'], $this->dbConfig['mypwd']); }}?>
To fdipzone:
I used the array in your second method, and there was a problem. during debugging, the final output array print_r ($ this-> dbConfig) of the constructor );
Output result: Array ([myhost] => localhost [myuser] => cccc [mypwd] => 123456) array ([myhost] => [myuser] => [mypwd] =>)
The output is two arrays. The first is normal and the first value is null,
Then I output the Array $ this->__ construct (); print_r ($ this-> dbConfig) in the method opendata. In this method, the output only displays: Array
Solving?
To fdipzone:
After another test, your second method uses an array to change require_once to include, and everything works normally. The problem is solved. why ??
#6. the code of solution 2 is incorrect!
Although it is a sample code, since it is to teach people how to do it, how should it be tested?
1. the loading parameter uses require_once, which indicates that the loaded data is loaded only once.
When two mydatabases need to be instantiated, only the first database can obtain the parameter.
2. in opendata, $ this->__ construct () is executed based on error 1. when the mydatabase method is executed, the database cannot be connected because the parameter file cannot be loaded.
Which of the following statements is true?
class mydatabase{ private $linkid; public function __construct(){ $dbConfig = include("../data/a.php"); $linkid = mysql_connect($dbConfig['myhost'], $dbConfig['myuser'], $dbConfig['mypwd']); } function opendata($database, $charset='utf8'){ mysql_select_db($database, $this->linkid); mysql_query("set names $charset", $this->linkid); }}
1. Parameter files are sensitive and should not be stored in objects. Load as needed and drop when used up
2. a database class corresponds to only one database connection, so the database connection should be completed in the constructor.
3. the opendata method is only responsible for selecting databases and character set settings, with a single task
I would like to thank the moderators and experts for their enthusiastic help. thank you very much !!