The following two sections of code explain why the execution results are different. thank you. $ UserName = "root ";
$ Userpwds = "1234 ";
$ DbName = "test ";
$ ServerName = "localhost ";
// Connect to the database
$ Conn = mysql_connect ("localhost", "root", "1234 ");
// Select a database
$ Select = mysql_select_db ("test", $ conn );
If ($ select ){
Echo "database connection successful ";
}
?>
Different from the following code execution results, please analyze $ UserName = "root ";
$ Userpwds = "1234 ";
$ DbName = "test ";
$ ServerName = "localhost ";
// Connect to the database
$ This-> conn = mysql_connect ($ this-> serverName, $ this-> userName, $ this-> userPwd );
// Select a database
$ This-> my_db = mysql_select_db ($ this-> dbName, $ this-> conn );
If ($ this-> my_db ){
Echo "database connection successful ";
}
?>
Reply to discussion (solution)
$ This->
What is the complete code of this class in your class?
$ This-> serverName
Is it an attribute in a class? Are there any settings?
If you are sure you have pasted all the code, we recommend that you check the PHP class.
What is the key difference?
Class login
{
Private $ userName = "root ";
Private $ userpwds = "1234 ";
Private $ dbName = "test ";
Private $ serverName = "localhost ";
Function _ construct ()
{
// Connect to the database
$ This-> conn = mysql_connect ($ this-> serverName, $ this-> userName, $ this-> userPwd );
// Select a database
$ This-> my_db = mysql_select_db ($ this-> dbName, $ this-> conn );
Mysql_query ("set names gb2312"); // sets the encoding format
If ($ this-> my_db ){
Echo "database connection successful ";
}
}
?>
If the connection is successful, "database connection succeeded" will be displayed. why is the result incorrect? the program is incorrect. please modify it. thank you.
Class login
{
Private $ userName = "root ";
Private $ userpwds = "1234 ";
Private $ dbName = "test ";
Private $ serverName = "localhost ";
Function _ construct ()
{
// Connect to the database
$ This-> conn = mysql_connect ($ this-> serverName, $ this-> userName, $ this-> userPwd );
// Select a database
$ This-> my_db = mysql_select_db ($ this-> dbName, $ this-> conn );
Mysql_query ("set names gb2312"); // sets the encoding format
If ($ this-> my_db ){
Echo "database connection successful ";
}
}
?>
If the connection is successful, "database connection succeeded" will be displayed. why is the result incorrect? the program is incorrect. please modify it. thank you.
What is the error?
Program according to the code you give
The definition of the login class is not complete. an error will be reported when you execute it separately.
Parse error: syntax error, unexpected end of file
Let me modify it and let it output the words "database connection succeeded". thank you.
At?> Before joining
}new login;
No, it cannot be tested. there is nothing after the test. Test it again. okay. Thank you.
Very good. I made a mistake. thank you.
The moderator is really serious.