I have been trying to connect my PHP file to MySQL database for a while, now I'm using WAMP server and encountering error 1045, I have also setup a password and tried various settings given on net.
this is the scenario
<?php
$link = mysqli_connect("127.0.0.1", "root", "password", "registration");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
here is the code I'm running, registration is the database,I have created it using phpmyadmin
<?php
$link = mysqli_connect("127.0.0.1", "root", "", "registration");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
Related
I have mysql 8.0.15 for windows and I'm trying to connect from remote php webSite but always getting
2002: Connection refused
The code I use to test the connection is very simple
$dbHost = "HOST";
$dbUser = "root";
$dbPass = "password";
$dbDB = "DBNAME";
$dbPort = "9999";
echo "Conectando a $dbHost,$dbUser,$dbPass,$dbDB,$dbPort ";
$link = mysqli_connect($dbHost,$dbUser,$dbPass,$dbDB,$dbPort);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
I also try host:port and p:host:port and always getting the same error message.
If you want to change the MySQL port, you have to edit the my.ini config-file on the MySQL server by setting port=<YOUR PREFERRED PORT>, then check if the port is forwarded and finally restart the MySQL server.
I have Apache (with PHP) and MySQL installed and running on my Raspberry Pi. I've done some simple tests with PHP, and it seems to be working. And MySQL is working perfectly from the terminal, and even another computer.
I made this PHP file:
<?php
echo("Connecting");
$connection = new mysqli("127.0.0.1", "admin", "password", "test");
if ($connection->connect_error) {
die("Connection error");
}
echo("Connection successful");
?>
Yet when I go to this page in my web browser, all I see is "Connecting". If I comment out the connection command, I see "Connecting Connection successful" in the browser.
It seems as if the PHP code stops running or hangs at the connection command.
Any ideas why I'm having this strange behavior?
Try adding these lines at the top: error_reporting(E_ALL);
ini_set('display_errors',1);
I hope You should do following
if ($connection->connect_error) {
echo "Connection error";
}else{
echo "Connection successful";
}
Try this instead
<?php
$link = mysqli_connect("127.0.0.1", "admin", "password", "test");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
When running below code it shows error like this
$link = mysqli_connect($server, $user, $password, $db);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
Warning: mysqli_connect(): Headers and client library minor version mismatch. Headers:50552 Library:50631
I am using php 7.1.2
I have a script that connects to a MySQL database in localhost, when I run it from the console it runs just fine, but when I run it from the web, I get this:
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /var/www/html/tests/mysql.php on line 3
Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: No such file or directory
I checked my php.ini file and it has the correct location of mysql.sock, the file exist, but php won't connect via web.
Also, I have Fedora Linux with Firewall disabled as well as SELinux, so I have no idea what can be causing this.
This is the test code I'm using:
<?php
$link = mysqli_connect("localhost", "root", "password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
Any ideas?
I am looking to see where I place the mysqli replacement code so that I can eliminate the connect errors I get...
function DBLogin()
{
$this->connection = mysql_connect($this->db_host,$this->username,$this->pwd);
if(!$this->connection)
{
$this->HandleDBError("Database Login failed! Please make sure that the DB login credentials provided are correct");
return false;
}
if(!mysql_select_db($this->database, $this->connection))
{
$this->HandleDBError('Failed to select database: '.$this->database.' Please make sure that the database name provided is correct');
return false;
}
if(!mysql_query("SET NAMES 'UTF8'",$this->connection))
{
$this->HandleDBError('Error setting utf8 encoding');
return false;
}
return true;
}
I have the following example, just not sure what I have to remove from the code above to get it to work. Any help?
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>