I created a working web app with login/register etc using a local dev server/wamp on my laptop, but require to move it all to linux server on a different system. I recreated the database identically in phpMyadmin and i am using the following connection string:
<?php
$server = '150.204.20.22';
$user = 'enramclo';
$pass = '***';
$dbname = 'enramclo';
$dbcon = mysqli_connect($server, $user, $pass, $dbname)or die("Can not connect to Server.");
?>
All these details are correct, but it simply will now not connect. I have little experienc eof linux so is there something I need to know as to why it will not work?
What is the os type? You might have SELinux enabled. Try to execute in shell and see if it's works
setenforce Permissive
Related
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();
}
I recently set up RDS server using MySQL dataset and using EC2 beanstalk as a server.
I have been able to connect to dataset from
console of local machine using endpoint url, username, password
from PHP on local machine, using following code:
$servername = "xxxxx.xxxxxxxxxxxxxx.us-east-1.rds.amazonaws.com";
$username = "xxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxx";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname, 3306);
// Check connection
if (! $conn) {
echo "not connecting..!!";
die("Connection failed: " . mysqli_error($conn));
}
This works correctly, inserts data into the database in the RDS.
I ssh to EC2, check connect from console (using mysql -h -u -p). This too, work correctly. I can change the database from here and it is indeed reflected.
Next, I deploy the exact same code on EC2, and hope it works via PHP as well, as that code is already tested. But it does not.
I checked SELinux status is disabled
What else could be a problem?
I should mention: I am using Amazon Linux, I installed 'httpd' on local it.
Hey guys I have problem with connecting my .php file to database, I've tried different ways but still the same response.
Here is my code (updated):
<?php
$server_name = "localhost";
$mysql_username = "*****";
$mysql_password = "*****";
$db_name = "*****";
$conn = mysqli_connect ($server_name, $mysql_username, $mysql_password, $db_name);
if($conn) {
echo "Success";
}
else {
echo "Failed";
}
?>
UPDATE
Guys, when I connect to localhost everything works fine but I would like to connect to a server and be able to upload data from different android devices. Currently I use 000webhost.com free service, so might it be the key point and problem that I didn't upgrade that to PRO?
mysqli_connect requires the arguments to be in the order servername, username, password, databasename. Your order is servername, databasename, username, password.
The code should be:
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
You sequence of parameter is wrong
<?php
$server_name = "*****";
$db_name = "*****";
$mysql_username = "*****";
$mysql_password = "*****";
$conn = mysqli_connect($server_name, $mysql_username,$mysql_password, $db_name);
if($conn) {
echo "Success";
} else {
echo "Failed";
} ?>
Correct syntax
mysqli_connect(host,username,password,dbname,port,socket);
Reference mysqi_connect
EDIT
A extra tip
$conn = mysqli_connect("localhost","username","password","db_name");
Put values according to you server.
EDIT 2
MySQL needs to be running on the port 3306 make sure it is correct
From the comments and from the ruling out of other possibilities indicated in the other answers, it is quite possible it is a connection issue with your service provider.
You should write to them explaining your situation, if at all they have support for free services. You should probably change the passwords to some dummy ones in case you have share it with them; Some service providers ask you to share your password for them to check, not sure if it's recommended practice.
Also, for testing and development, it's better you use some kind of local installation of PHP, such as XAMPP or WAMPP etc, so that you can have control during development and testing. If something works on your local installation and not on the remote, having a development installation is useful to isolate and report such issues.
I think your problem is that you did not instantiate the class, remedied by making it $conn = new mysqli ($server_name, ..... . Also, your sequence of parameters is incorrect. It should be server, user, password and finally database.
In addition, you should be using PHP constants instead of variables for this, so $server_name should be SERVER_NAME. Similarly $mysqli_username, $mysqli_password, and $db_name should be changed. Not that I wouldn't work as you have it, but it just is not proper.
Iy you are using MAMP, the default port for mysql is 8889, but the "traditional" port that php expects to use for mysql is 3306.
So you need to open MAMP and change the MAMP mysql port to 3306, then restart the mysql server. Now the connection should be successful.
I have a SQL server on MS azure. Using a php file and some basic select statements (eg. select * from table) I have been able to grab data from the database, so I know my details are correct.
However when I put a connect statement in any other files the page appears blank. For example, any form submission pages.
This is the connect statement I have:
<?php
$myServer = "hostname";
$myUser = "username";
$myPass = "password";
$myDB = "dbname";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
?>
I'm sure it's probably something simple but I have tried loads of different ways and nothing seems to work, I don't even get an error message to work with.
Could someone please help?
Uncomment this line in your php.ini.
extension=php_mssql.dll
To locate your php.ini file, add this code below at the top of your php script
<?php phpinfo(); ?>
By default, Azure environment doesn't install php_mssql.dll extension, it installs php_sqlsrv.dll instead.
You can leverage Console tool in Azure portal and execute php -m to check all the preinstalled modules in Azure PHP runtime:
You can use PDO in PHP to handle sql server database, such as the following connection test:
try {
$conn = new PDO("sqlsrv:server = tcp:garysql.database.windows.net,1433; Database = garymbdata", "gary", "QWEasdzxc123");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
print("Error connecting to SQL Server.");
die(print_r($e));
}
Additionally, we can configure PHP configuration settings on Azure Web App beside modifying php.ini directly. Also we can install mssql.dll extension on Azure Web App if you insist on.
Please refer to https://learn.microsoft.com/en-us/azure/app-service-web/web-sites-php-configure for more info.
I try connect my remote server database in php but it's give bellow error
Host 'xx.xxx.xx.xx' is not allowed to connect to this MariaDB server in
My connection code like this
$servername = "my_server_address";
$username = "my_username";
$password = "my_password";
$dbname = "my_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
If your credentials are valid, you most likely need to configure MariaDB for remote client access.
See Configuring MariaDB for Remote Client Access
I had a similar issue after installing "xampp-windows-x64-7.3.6". After looking around and testing several solutions without any changes I did the following:
Uninstall xampp
Restart machine
Reinstall xampp
Restart machine
Importing old databases via phpmyadmin
All works fine for me now.
I did initially add a new user account. That might have caused the problem but I'm not testing this again.
You need to add new user in mariadb, goto privileges and add that can be accessed remotely