PHP connection MySQL is unsuccessful and can be successfully connected using MYSQLI and PDO. How to fix it

Source: Internet
Author: User
PHP connection MySQL is unsuccessful and can be successfully connected using MYSQLI and PDO.
Environment is win7 under their own manual build Apache2.2+php5.3+mysql5, the environment is built successfully, phpinfo see above the MySQL and mysqli are right, but using PHP connection MySQL is not successful, However, you can successfully use MYSQLI and PDO, and the connection error is not to display error messages, jump directly to inaccessible pages. Request Help!!!!!!!!


------Solution--------------------
PHP Code
Function Connect ($servername, $username, $password, $dbname) {echo "Enter 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 re-use a connection to the MySQL server.

Parameters

Server
MySQL server. You can include a port number, such as "Hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.

If PHP directive mysql.default_host is undefined (by default), 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 a blank password.

New_link
If you call mysql_connect () for the second time with the same parameters, no new connection will be made and the connection ID that is already open will be returned. The parameter New_link changes this behavior and causes mysql_connect () to always open a new connection, even when mysql_connect () was previously called with the same parameters.

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. See MySQL client constants for further information.

return value

Returns a MySQL connection ID if successful, and FALSE if it fails.

Update log

Release Notes
4.3.0 Add the Client_flags parameter.
4.2.0 Add the New_link parameter.
3.0.10 Add ":/path/to/socket" support to the server.
3.0.0 adds ":p ort" support 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 ' Connected successfully ';
Mysql_close ($link);
?>
Example #2 mysql_connect () Example: using Hostname:port syntax
We connect to example.com and port 3307
$link = mysql_connect (' example.com:3307 ', ' mysql_user ', ' Mysql_password ');
if (! $link) {
Die (' Could not connect: '. Mysql_error ());
}
Echo ' Connected successfully ';
Mysql_close ($link);

We connect to localhost at Port 3307
$link = mysql_connect (' 127.0.0.1:3307 ', ' mysql_user ', ' Mysql_password ');
if (! $link) {
Die (' Could not connect: '. Mysql_error ());
}
Echo ' Connected successfully ';
Mysql_close ($link);
?>
Example #3 mysql_connect () Example: Using 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 ' Connected 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 ' Connected successfully ';
Mysql_close ($link);
?>
Comments

Note:

As long as the server is designated as "localhost" or "localhost:port", the MySQL client library crosses this value and attempts to connect to the local socket (the name pipeline in Windows). If you want to use TCP/IP, you should 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:

At the end of the script, the connection to the server is closed, unless Mysql_close () has been explicitly called before.

Note:

You can suppress the error message when an error occurs by adding an @ to the function name.

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? Try it with your computer name and you've had problems like this 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.