How can I get PHP to connect locally to MySQL? - php

I'm trying to test a PHP website locally on an Ubuntu 16.04 machine, but I'm having trouble connecting to MySQL database using mysqli_connect().
I have Apache 2.4 running with mod_php7.0.
I installed the Ubuntu package php7.0-mysql.
Accessing a php.info() file at a localhost address in the same directory as the website indicates that my php.ini file is /etc/php/7.0/apache2/php.ini.
In that file, I removed the comment from the line ;extension=php_mysqli.dll.
The file I'm trying to access contains the line
$con=mysqli_connect("localhost","db_user","db_password","db_name");
When I try to access the site, I get the error
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known"
with reference to the line above.
I have MySQL running locally with the given user, password, and database defined. I've tried using 127.0.0.1 instead of localhost, but the error is the same.
The error appears to be that PHP can't make a local connection to MySQL. How can I figure out what's wrong?
Edit To give more context, I'm trying to create a localhost version of a website that works on its production/dev servers. As far as I can tell, I've recreated all relevant aspects of the server setup, but obviously I've missed something.
The biggest difference between the local setup and production/dev version is that I have PHP7 locally but PHP5.5.9 on production/dev. My best guess at this point is that this difference is what's responsible for the localhost failure, but I don't know enough about PHP to narrow down what it might be.

Use the following to connect to MySQL database on localhost.
<?php
$servername = "localhost";
$username = "root";
$password = "mysql-password";
$dbname = "database-name";
$conn = mysqli_connect($servername, $username, $password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>

Related

SQL Debug Message: 2002) No connection could be made because the target machine actively refused it

I'm getting the almost infamous
(HY000/2002): No connection could be made because the target machine actively refused it
Connection failed: No connection could be made because the target machine actively refused it.
error messages in trying to get PHP to talk to MySQL.
Configure
PHP is 7.3.7 (NTS MSCV15 (Visual c++ 2017) x64)
MySQL is 8.0.17 on a localhost (127.0.0.1)
IIS Windows 10 (10.0.18362.1)
All of this is running locally on laptop at 127.0.0.1 (though the corporate network is a 10.0.?.?)
IT support has spent the best part of 2 hours looking at all network and firewall issues (to the point disabled all firewall and enabled basically all the ports) and nothing seems to get through
MySQL is running, the username and passwords are perfectly fine, the hosts file has a correct DNS entry and the code is correct.
<?php
$servername = "rackforms";
$username = "rackforms";
$password = "????????";
$port = "3312";
// Create connection
$conn = new mysqli($servername, $username, $password, $port);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
MySQL port has been deliberately moved to 3312. Use other MySQL instances on 3306. Really it shouldn't make difference.
I've looked at XAMPP and WAMP and other *AMP entries without much luck and spent way too much time on SO.
Any suggestions on what else to do?
Thanks
RESOLVED!!
Need to change the mysql.default_port in php.ini to the same port I am using for MySQL database. Not sure if there is a way to override this port using a connection string as (at least on Windows) seems to be ignored based on comments in php.ini

(PHP) No connection could be made because the target machine actively refused it

I'm creating a PHP application and have a local test enviroment on both my laptop and desktop.
EDIT: Generated logs in a pastebin.
I am using the WPN-XM Serverstack.
Everything worked fine beforehand, but now on my laptop, when I try to do anything that requires a connection with the database, it throws this error.
No connection could be made because the target machine actively refused it
Did some research and found out that it could be a problem with my firewall, turned it off, same result.
Read elsewhere that this is not an issue with my code, this makes sense since everything works on my desktop.
Things I've tried so far:
Restarted the webserver
Restarted the laptop
Turned off the firewall
Login to phpMyAdmin (Error: Cannot log in to the MySQL server)
Changed the port of the webserver to 8080
Any idea what might be causing this error?
For completeness, altough the problem is more than likely not in the code, here' s the connection file.
$name = "root";
$pass = "";
$db = "myDB";
$host = "localhost";
$connect = mysqli_connect($host, $name, $pass, $db);
if (mysqli_connect_errno()) {
echo "Could not connect to the mysql database. Error: " . mysqli_connect_error();
}

Establishing database Connection in php where mysql is in program files not in xampp

When I am installing XAMPP I have not installed MySQL where I have already using this database so I have not installed using xampp but in my PHP code when I am establishing connection I wrote
$conn = mysqli_connect($servername, $username, $password, $dbname);
At $servername, everyone is keeping localhost and explaining PHPMyAdmin examples where MySQL is installed in xampp
In my case MySQL is at c:/programfiles/MySQL what I have to give at my $servername
Please help me, I am new to programming
localhost isn't the name of your MySQL server's install directory. It's a reference to your local machine aka 127.0.0.1. Your server name is still localhost.
The only time you'll change this is if you host the database on a different machine.

PHP7 Not connecting to Microsoft SQL Server 2012

I have been searching for an answer for last 3 days before posting.
I have installed SQL Server 2012 and, Apache24, PHP7.0
The Apache started fine, PHP7 Started fine
I can browse localhost/info.php and it shows perfect
Installed ODBC
Installed the SQL extensions drivers
Configured PHP.ini and apache conf file
SQL services started through SQL configuration manager
also allowed in the network configuration the TCP\IP and named pipes
I can't login to the SQL server using Microsoft SQL server manager
So all seems to be fine but every time I try to do a simple PHP code to connect my PHP to the SQL server I get the below error
Warning: mysqli_connect(): (HY000/2002): No connection could be made
because the target machine actively refused it. in
C:\Apache24\htdocs\index.php on line 13 Could not connect
Have disabled the firewall also and even added a rule with few ports
I am running on windows 10 Enterprise
Also when I change the server name to WALEED\SQLEXPRESS I get the below
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo
failed: No such host is known. in C:\Apache24\htdocs\index.php on line
13
Tried to search in the SQL server log but couldn't find any errors
All extensions for my PHP are working fine on info.php
Index file :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'admin2017';
$db = 'sunto';
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db);
if(! $conn){
die('Could not connect') ;
}
echo 'Successfully Connected';
$sql = 'Connected Successfully';
$retvalue = mysqli_query($sql);
if(! $retvalue){
die('Cannot connect to SQL: ');
}
echo 'DataBase test_db13 has successfully created';
mysqli_close($conn);
?>
In the comments you said:
what is strange when i Try to connect with sqlsrv_connect(
"localhost", $connectionInfo ); it is fine connected but when i try to
connect using mysqli. it shows error
There's nothing strange about that. The clue's right there in the name of the method you're using:
mysqli_connect()
That code library is for connecting to MySQL databases only.
You need to use the sqlsrv_ library instead if you want to connect to SQL Server. They're completely different database platforms, with completely different PHP libraries to use for working with them. As you saw yourself, it works when you use sqlsrv_connect.
I'm not really sure why you then tried to use mysqli after that, or why you expected that to be successful.

Connecting MySQL database to website with PHP

As the title indicates I'm trying to link my MySQL database to my website using php.
I'm using GoDaddy hosting for the MySQL database and I got this code directly from them and it's still not working for some reason, giving the error below.
Here's my code :
<?php
$hostname = "trdlibrary.db.6253425.hostedresource.com";
$username = "*******";
$password = "*******";
$dbname = "trdlibrary";
$connect = mysqli_connect($hostname, $username, $password, $dbname, 3306) OR DIE ("Unable to connect to database! Please try again later or contact an administrator for help.");
?>
Here's the error :
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed
because the connected party did not properly respond after a period of
time, or established connection failed because connected host has
failed to respond. in C:\xampp\htdocs\searchengine\dbconnect.php on
line 6 Unable to connect to database! Please try again later or
contact an administrator for help.
Can anyone see the issue?.. or is this something I need to contact GoDaddy's support line about.
-- EDIT --
I forgot to mention that I am running the web server off my local machine using XAMPP (so I can run the PHP scripts) but the MySQL server is off-site with GoDaddy, so 'localhost' won't work currently.
-- EDIT #2 --
So I learned that sadly, remote access of MySQL databases to GoDaddy requires a more expensive and 'higher tier' hosting package from them. So I just answered my own question, thanks to those who replied.
You have to setup remote access to databases on Godaddy. You can refer to this page to see how to enable it.

Categories