The following two sections of code explain why the execution results are different. thank you.

Source: Internet
Author: User
Tags parse error
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.