Php cannot connect to Mysql. you can use Mysqli and PDO to connect to Mysql successfully. How to solve

Source: Internet
Author: User
Php cannot connect to Mysql. you can use Mysqli and PDO to connect to Mysql successfully. The environment is self-built apache2.2 + php5.3 + mysql5 under win7. all environments are successfully built. Mysql and Mysqli shown on phpinfo are correct, but connection to Mysql using php is unsuccessful, however, Mysqli and PDO can be successfully used, and the connection error is that no error message is displayed. directly jump to the inaccessible page, and the connection to Mysql using php is unsuccessful. you can use Mysqli and PDO to connect successfully.
The environment is self-built apache2.2 + php5.3 + mysql5 under win7. all environments are successfully built. Mysql and Mysqli shown on phpinfo are correct, but connection to Mysql using php is unsuccessful, however, Mysqli and PDO can be successfully used, and the error message is not displayed when the connection is incorrect. you can directly jump to an inaccessible page. Request help !!!!!!!!


------ Solution --------------------
PHP code
Function connect ($ servername, $ username, $ password, $ dbname) {echo "access link"; $ con = mysql_connect ($ servername, $ username, $ password ); $ con = mysql_select_db ($ dbname, $ con); return $ con ;}
------ Solution --------------------
Mysql_connect

(PHP 4, PHP 5)

Mysql_connect-open a connection to the MySQL server

Description

Resource mysql_connect ([string $ server [, string $ username [, string $ password [, bool $ new_link [, int $ client_flags])
Open or reuse a connection to the MySQL server.

Parameters

Server
MySQL server. It can include the port number, for example, "hostname: port", or the path to the local socket, for example, ":/path/to/socket" for localhost ".

If the PHP command mysql. default_host is not defined (the default value is true), the default value is 'localhost: 3306 '.

Username
User name. The default value is the user name of the server process owner.

Password
Password. The default value is null.

New_link
If the same parameter is used to call mysql_connect () for the second time, a new connection is not established, but an opened connection id is returned. The new_link parameter changes this behavior and makes mysql_connect () always open a new connection, even when mysql_connect () was previously called with the same parameter.

Client_flags
The client_flags parameter can be a combination of the following constants: MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE, or MYSQL_CLIENT_INTERACTIVE. For more information, see MySQL client constants.

Return value

If the connection succeeds, a MySQL connection id is returned. if the connection fails, FALSE is returned.

Update Log

Version description
4.3.0 add the client_flags parameter.
4.2.0 add the new_link parameter.
3.0.10 add ":/path/to/socket" to the server.
3.0.0 add ": port" to the server.
Example

Example #1 mysql_connect () Example
$ Link = mysql_connect ('localhost', 'MySQL _ user', 'MySQL _ password ');
If (! $ Link ){
Die ('could not connect: '. mysql_error ());
}
Echo 'ccted successfully ';
Mysql_close ($ link );
?>
Example #2 mysql_connect () Example: Use the hostname: port syntax
// We connect to example.com and port 3307.
$ Link = mysql_connect ('example. com: 000000', 'MySQL _ user', 'MySQL _ password ');
If (! $ Link ){
Die ('could not connect: '. mysql_error ());
}
Echo 'ccted successfully ';
Mysql_close ($ link );

// We connect to localhost at port 3307
$ Link = mysql_connect ('127. 0.0.1: 8080', 'MySQL _ user', 'MySQL _ password ');
If (! $ Link ){
Die ('could not connect: '. mysql_error ());
}
Echo 'ccted successfully ';
Mysql_close ($ link );
?>
Example #3 mysql_connect () Example: use the ":/path/to/socket" syntax
// We connect to localhost and socket e.g./tmp/mysql. sock

// Variant 1: ommit localhost
$ Link = mysql_connect ('/tmp/mysql', 'MySQL _ user', 'MySQL _ password ');
If (! $ Link ){
Die ('could not connect: '. mysql_error ());
}
Echo 'ccted successfully ';
Mysql_close ($ link );


// Variant 2: with localhost
$ Link = mysql_connect ('localhost:/tmp/mysql. sock ', 'MySQL _ user', 'MySQL _ password ');
If (! $ Link ){
Die ('could not connect: '. mysql_error ());
}
Echo 'ccted successfully ';
Mysql_close ($ link );
?>
Note

Note:

As long as the server is specified as "localhost" or "localhost: port", the MySQL client library will overwrite this value and try to connect to the local socket (name pipeline in Windows ). To use TCP/IP, replace "localhost" with "127.0.0.1 ". If the MySQL client library tries to connect to an incorrect local socket, you should set the correct path in the PHP configuration and leave the server blank.

Note:

When the script ends, the connection to the server is closed unless mysql_close () has been explicitly called before.

Note:

You can add @ Before the function name to suppress error messages when an error occurs.

See

Mysql_pconnect ()-open a persistent connection to the MySQL server
Mysql_close ()-close MySQL connection
------ Solution --------------------
Do you use localhost or 127.0.0.1 for your connection Server Name? replace it with your computer name. Have you ever encountered this problem before?
------ Solution --------------------

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.