PHP, Failed connecting to database
Wampserver 3.2.0 new instalation or upgrading
This might help others
Probably xamp
using mariaDB
as default too.
Wamp
server comes with mariaDB
and mysql, and instaling mariaDB
as default on 3306 port.
To make mysql
work!
On instalation it asks to use mariaDB
or MySql
, mariaDB is checked as default and you cant change it, check mysql
option and install.
when instalation done both will be runing mariaDB
on default port and mysql
on another port.
Right click on wamp
icon where its runing should be on right bottom corner, goto tools and see your mysql
runing port.
And include in your database connection same as folowng :
$host = 'localhost';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$port = '3308';
$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
Note :
I am using pdo.
See here for more : https://sourceforge.net/projects/wampserver/
PHP MySQL connection failed
You can check that the MySQL server is bound to port 3306 using tcpview. More simply, drop the port from the host specifier. The driver should then attempt to use a named pipe, rather than a TCP socket.
On an unrelated note, I strongly urge you to switch to the PDO MySQL driver. The one you're using is terribly out of date. One big advantage is PDO supports prepared statements, which offer security and efficiency benefits.
Edit:
This doesn't answer your main question, but posting this information in a comment would be a mess.
Rather than W3Schools, check out the resources suggested in:
- What is the best PHP programming book?
- PHP tutorial that is security-, accuracy- and maintainability-conscious?
- Good Resources for Relational Database Design
- Relational database theory and SQL book recommendations?
- How should a programmer learn great database design?
- Database Design Best Practices
- Best Book for a new Database Developer
PHP connecting to database fails without error
There are multiple ways to handle errors
Simplest of all, use try catch while connecting
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
try {
$conn = new mysqli($servername, $username, $password); } catch(\Exception $e) { var_dump ('oopss... this is the error', $e)}
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected
Database connection failed badly Error
This error is basically saying the host cant be found. Are you using 'localhost' as the ip to connect to the database. If not check the ip of the database that you should be connecting with.
Related Topics
Session_Start() Creates New Session Every Refresh
Why Are Escape Characters Being Added to the Value of the Hidden Input
Dealing with Special Characters in Object Property Names
Blank Spaces in Column Names with MySQL
How to Return JSON Data from PHP Mongocursor
PHP Object of Class Dateinterval Could Not Be Converted to String
Instagram Login Programmatically
How to Detect and Handle MySQL Warnings with PHP
Use Curl with Sni (Server Name Indication)
Variable Variables Pointing to Arrays or Nested Objects
Object Oriented PHP Best Practices
How to Write to the Console from a Laravel Controller