It is my first time using PHP. I am using XAMPP on a mac.
MySQL, Apache, and localhost:8080 are all working right now.
I created this file called test.php and saved it inside lampp/htdocs:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Connection</title>
</head>
<body>
<?php
$servername = "localhost:8080";
$username = "root";
$password = "root";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error($conn));
}
else
{
echo "Connected successfully";
}
?>
</body>
</html>
However, when I go to localhose:8080/test.php a blank page opens but nothing shows up
Port 8080 is for your web server. Your MySQL server listens by default on port 3306. You can check in your XAMPP settings if the port is different, but it should be 3306. This means that you can omit the port number.
To connect to the database using mysqli you only need 3 lines of code:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'pass', 'db_name', 3306); // you can omit the last argument
$mysqli->set_charset('utf8mb4'); // always set the charset
The servername argument to mysqli_connect been to be just the servername.
If you want to supply a port number then that goes in the fifth argument to mysqli_connect. It isn't mixed into the first.
That said:
since localhose:8080/test.php shows a web page, it is clear that you are running your HTTP server on port 8080, not your database server.
for performance reasons you should prefer using a socket connection to MySQL/MariaDB, not a TCP/IP port
Related
I am setting a new live server for my Laravel application its work perfectly on localhost but not on live server. Now its showing SQL State [2002] connection refused. I also try with mysqli_connect and PDO but the error remains the same. Is possible a problem with Hosting Provider?
<?php
$servername = "examrunner.com";
$database = "XXXXXXXX";
$username = "XXXXXXX";
$password = "XXXXXXXXXX";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Use server name as localhost. Because cPanel hosts your databases locally, use localhost as the database's hostname.
$servername = "localhost";
Please check below mentioned points to resolve this issue :
Check database and user exists
Make sure that Database user and Database connected to each other and you have given sufficient privileges to user.
If site hosted on current serve user hostname = 'localhost'
Try to print detailed error.
I have a web server running on a raspberry pi model B, i have port forwarded port 80 for http and this is successfully working. However, because there is a login page on my site, i need mysql. How do i connect my current site using php to a mysql date base, will i need to port forward the mysql port?
i've pasted the code below:
<?php
/*PHP for connecting website to database*/
$servername = "ip/hostname";
$database = "database";
$username = "username";
$password = "password";
$conn = mysqli_connect($servername, $username, $password,
$database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
No, you will not need port forward. It's not the user who is logging in into mysql database, it's the php server doing the connection.
As a server hostname in your php file you can simply use localhost, if the mySQL db and php server are on same device/machine.
I have a website that has a page that needs to show all the information that is in the local MySQL server (not in the hosting). How am I gonna do that?
I only tried to change the connection details but no luck on that matter.
db_connect.php (connect to hosting server)
<?php
$servername = "localhost";
$username = "xxx";
$password = "yyy";
$database = "dbdb";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error){
die("FAILED TO CONNECT : " . $conn->connect_error);
}
?>
to db_local.php (connect to my local server)
<?php
$servername = "192.168.x.xx";
$username = "";
$password = "";
$database = "info";
$conn = new mysqli($servername, $username, $password, $database);
if($conn->connect_error){
die("FAILED TO CONNECT : " . $conn->connect_error);
}
?>
Is it possible? I only need to do that as a first aid solution in my main problem. I am getting this kind of error:
FAILED TO CONNECT : Can't connect to MySQL server on '192.168.x.xx' (110)
mysql might only accept connections from localhost, if you are connecting to the db from your local computer then you need to change the address to localhost
If you are not connecting to the DB via local machine then you need to make sure that port 3306 is open on the server, selinux is off ( assuming you are on linux ) and that you have the proper credentials.
If that doesnt work edit your my.cnf and change the line bind-address: 127.0.0.1 to the machines local ip.
Hope this works...
https://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
connecting to mysql server on another PC in LAN
My system using core php and it is working fine in localhost. When I host into live then there is an error occurs site not working. I hosted in GoDaddy server and below are the code.
//connection code
<?php
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "******";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
But i get below error in browser
PHP Warning: mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it.
in G:\PleskVhosts\projectname.com\httpdocs\pages\header.php on line 13
Please help me to fix it.
Not a problem in your code. May be service is not listing on your port or firewall is blocking your connection. In this case most possible reason is firewall.
You need to try "netstat -anb" for checking a running expected port. Here is a link my be help you:
No connection could be made because the target machine actively refused it?
I am trying to use mysqli_connect as such:
$host = "ftp.tatiana-renae.com";
$user = "tatiana";
$password = "********";
$dbname = "********";
$connection = mysqli_connect($host, $user, $password, $dbname);
but I get this error:
Database connection failed: Can't connect to MySQL server on 'ftp.tatiana-renae.com' (111) (2003)
I know mysqli_connect works differently than mysql_connect but I can't figure out what to do for mysqli_connect to make it happy. Any ideas?
Error 111 is "connection refused". One candidate explanation is that MySQL is not "listening" on the network, but only the TCP loopback address.
Check the my.cnf for the server, and check if it contains lines like this:
skip-networking
or
bind_address=127.0.0.1
If your PHP is running on the same server as MySQL, you can try using '127.0.0.1' for the host, in place of 'ftp.tatiana-renae.com'.
Is the MySQL server running? Can you connect to it using the mysql command line interface?
There's several other possibilities; but there's not enough information provided about you environment for us to accurately diagnose the problem. We're just guessing.