Home Backend Development PHP Tutorial Summary of solutions to the problem that php cannot connect to mysql_PHP tutorial

Summary of solutions to the problem that php cannot connect to mysql_PHP tutorial

Jul 13, 2016 am 10:56 AM
mysql php exist develop Summarize article method solve connect question

This article summarizes and shares some solutions to some problems that may often be encountered in PHP development when PHP cannot connect to the MySQL database. Friends in need can refer to it.

Question 1

I installed mysql and php on my notebook today, and everything was installed normally (php@IIS7.5-fastCGI). However, when using phpmyadmin to connect and test, an error of "#2002 Unable to connect" appears. The same problem occurs when using other programs.
After checking, the mysql extension of php is installed correctly and the mysql service starts normally--that's strange. I used navicat to connect again and it was connected. . It seems that there is a problem with the communication between php and mysql.
After searching on Baidu, the problem was found:
The mysql_connect() function database server parameter is currently set to localhost, but it can be accessed normally after it is changed to 127.0.0.1.
The system hosts file does not provide resolution from 127.0.0.1 to localhost.

Solution (taking win7 system as an example): Find the C:\Windows\System32\drivers\etc\hosts file. The default is read-only. You need to copy a copy to another path such as the desktop. Open the file (Open with: Notepad) and add:

at the end of the file

127.0.0.1 localhost

If it already exists, remove the "#" in front of it.

Question 2


PHP scripts can run normally, such as phpinfo().

As soon as the "mysql_connect" function appears, there will be no response and no error will be reported.

Such as $conn = mysql_connect("127.0.0.1","root","xxxx") or die("MYSQL database connection failed:".mysql_error());

appears in the apache log directory error.log

PHP Warning: PHP Startup: Unable to load dynamic library 'c:\\php\\ext\\php_mysql.dll'

Solution

Copy "libmysql.dll" to the bin directory of apache. Then restart and observe the error.log. If the error disappears, congratulations.

Another: Online information also says to copy ""libmysql.dll"" and "php_mysql.dll" to windows/system32.


Question 3

LoadModule php5_module "D:/ccopen/php5/php5apache2_2.dll" AddType application/x-httpd-php .php
The code is as follows
 代码如下 复制代码
$link_id=@mysql_connect( "localhost ", "sampadm ", "secret ");
//以下代码省略
?>
Copy code


$link_id=@mysql_connect( "localhost ", "sampadm ", "secret ");

//The following code is omitted ?>

The following prompt appears when running:

fatal error: call to undefined function mysql_connect() in var/www/html/mytest.php

Solution



1.php.ini

extension_dir is set to "d:\apache2\php\ext" absolute path, otherwise errors will occur in some environments.

Open the following two lines of function extensions

extension=php_mysql.dll
extension=php_mysqli.dll

2. Copy libmysql.dll (under the php directory) and php_mysql.dll (under the php/ext directory) to the c:\windows\system32 directory (libmysql.dll must be in the php directory, for example, mine is D:\apache2\ libmysql.dll in the php directory, php_mysql.dll is in D:\apache2\php\ext)

3. Test,

1> Open the command line first

A) Click the "Start" menu-> "mysql"-> "mysql5 server"-> "MySQL Command Line Client"
//If this step is successful, mysql security is successful. If it fails, the mysql installation fails. Please uninstall and reinstall

B) Enter password
//If this step is normal, it means that the password of the root account is correct. If it fails, the account password is incorrect

2> Create a new file in the home directory and save it as testmysql.php, then enter http://localhost/testmysql.php (or http://localhost:81/testmysql.php) in the browser


If not, check if apache has loaded the php and mysql methods


To support php5 in apache, add the following three lines to the httpd.conf file

PHPIniDir "D:/ccopen/php5/"
Remove the following two lines of semicolons in php.ini extension=php_mysql.dll extension=php_mysqli.dll Copy php_mysql.dll and libmysql.dll to system32 Restart the apache server

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632173.htmlTechArticleThis article summarizes some problems that you may often encounter in PHP development and that PHP cannot connect to the MySQL database. Share the solution with friends who need it for reference. Question 1...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles