Xx...Tazer...xX Owner
Posts : 1239 T®ue Points : 1102678 T®ue Reputation : 40 Join date : 2009-09-02 Age : 31 Location : Chicago,Illinois
| Subject: MYSQL CONNECT INSTRUCTIONS AND ERRORS - PHP Sun Jul 11, 2010 7:28 pm | |
| here and today I will solve your problems of getting errors on MySql. Well this information will help you in any errors I have found out in PHP and how to avoid them errors. So in this information, I will show you so ways that users avoid this bad situation, to my way, so lets get started. Now I have a user name -iMmOrTaL(Programmer) has a fail problem, so today I will ask all of your questions(if I can). Now that many users of PHP has technical difficulties which is the MySql host server error but there is some way to bypassed it, so here is some solutions. But first, what is your Operating System Platform(Windows, Linux and etc)? And what is your MySQL error(Like #3306, #2006 and etc)? And for those who doesn't know "What is MySQL connection?", well here is a definition of it.
MySQL connection - Open a connection to a MySQL Server.
Ok here is some listed ways to stop the errors in MySql:
Windows: On Windows Vista or above, an entry in the Windows/System32/drivers/etc/hosts file causes mysql_connect() connections to "localhost" to timeout and never connect. This happens on php 5.3 and above since it now uses mysql native driver which has changed it connection behavior compared to libmysql.dll in previous versions. It is not a PHP bug, but definitely a configuration issue for users on new windows systems.
To get around this, you must remove the entry like this: ::1 localhost
and make sure you still have: 127.0.0.1 localhost
Also, you could change the code to connect to the ip instead, but that is inconvenient if you have many web sites.
This issue occurs on Windows Vista, Windows 7 and Windows Server 2008.
Note: that named pipe on Windows is unusable since PHP 5.3, and TCP connection shall be used even in localhost.
Mac OS X: If you are getting an error "Can't assign requested address" you may have a problem with the mysql port.The php.ini file suggests that PHP will select the port by using the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services, but in this case it is not so. Going into the /etc/php.ini file and setting the default port number to 3306 fixed the problem.
Code: [Select] mysql.default_port = 3306
This may cause a bug, which depends on what kind of PHP device your using. Most users found out 5.0-5.3 may have bugs but the community have fix it now for a newer version.
sedativechunk had made a connection class for MySql connection. It's MYSQL with OOP in mind and works similar to ASP.net methods for connecting to a database. The benefit of this class is easy management of multiple MYSQL connections.
Code: [Select] class Connection { var $db_connection = null; // Database connection string var $db_server = null; // Database server var $db_database = null; // The database being connected to var $db_username = null; // The database username var $db_password = null; // The database password var $CONNECTED = false; // Determines if connection is established
/** NewConnection Method * This method establishes a new connection to the database. */ public function NewConnection($server, $database, $username, $password) { // Assign variables global $db_connection, $db_server, $db_database, $db_username, $db_password; $db_server = $server; $db_database = $database; $db_username = $username; $db_password = $password;
// Attempt connection try { // Create connection to MYSQL database // Fourth true parameter will allow for multiple connections to be made $db_connection = mysql_connect ($server, $username, $password, true); mysql_select_db ($database); if (!$db_connection) { throw new Exception('MySQL Connection Database Error: ' . mysql_error()); } else { $CONNECTED = true; } } catch (Exception $e) { echo $e->getMessage(); } }
/** Open Method * This method opens the database connection (only call if closed!) */ public function Open() { global $db_connection, $db_server, $db_database, $db_username, $db_password, $CONNECTED; if (!$CONNECTED) { try { $db_connection = mysql_connect ($db_server, $db_username, $db_password); mysql_select_db ($db_database); if (!$db_connection) { throw new Exception('MySQL Connection Database Error: ' . mysql_error()); } else { $CONNECTED = true; } } catch (Exception $e) { echo $e->GetMessage(); } } else { return "Error: No connection has been established to the database. Cannot open connection."; } }
/** Close Method * This method closes the connection to the MySQL Database */ public function Close() { global $db_connection, $CONNECTED; if ($CONNECTED) { mysql_close($db_connection); $CONNECTED = false; } else { return "Error: No connection has been established to the database. Cannot close connection."; } }
} ?>
MySQL_Connection
Here is a description:
Code: [Select] resource mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )
Opens or reuses a connection to a MySQL server.
Also here are some descriptive parameters:
Server:
The MySQL server. It can also include a port number. e.g. "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for the localhost.
If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.
Username: The username. Default value is defined by mysql.default_user. In SQL safe mode, this parameter is ignored and the name of the user that owns the server process is used.
Password: The password. Default value is defined by mysql.default_password. In SQL safe mode, this parameter is ignored and empty password is used.
New link: If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.
Client flags: The client_flags parameter can be a combination of the following constants: 128 (enable LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE. Read the section about MySQL client constants for further information. In SQL safe mode, this parameter is ignored.
Here is a info of returning values: Returns a MySQL link identifier on success or FALSE on failure.
Example:
Example #1 - MySQL_connect() Code: [Select] $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?>
Thanks for reading and I hope this information helps.
Credits:_-_EVIL-LOCO_-_ and all other PHP experts. | |
|