Troubleshooting Connection to AWS RDS MySQL Database - php

I've been working on migrating a web application from a MySQL server hosted by my organization to an AWS RDS instance that I own. I've used the below code to confirm a successfull connection or display a relevant error message, but when this page loads, no output is displayed whatsoever:
<html lang="en">
<head>
<title>Reset Database to Default State</title>
</head>
<body>
<?php
$con=mysqli_connect("db_id.cx7cov75b6r5.us-west-2.rds.amazonaws.com:3306", "username", "pwd", "db_id");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else echo "Connected";
$script = file_get_contents("/home/username/public_html/reset.sql", false, null);
mysqli_multi_query($con, $script);
mysqli_close($con);
echo "Database State Reset <br>";
echo " <a href='home.html'>Main Menu</a>";
?>
</body>
</html>
I've confirmed that no messages appear in the console. Additionally, I added my webserver IP to the inbound rules for the RDS instance security group, and have since changed the inbound rule to accept all traffic (0.0.0.0/0) for the purposes of getting an initial connection.

Related

frequent lost of connection to mySQL server

I instal wamp server locally on my PC.I create a user registration form using php and mySQL database for pratice.I was able to connect to my mySQl data server and if i input data on the form and submit is work successfully.I normal experience at time when i access the user registration form it lost connection to my SQLdata server even though my wamp server is on.I will troubleshoot by changing some of the parameter by using mysqli_connect() to check if i will be prompted unsuccessful connection still nothing will show.When i check some other time it will work successfully.Please what may be causing this issue?
Run wampserver as administrator by right clicking wampserver.exe file. It may help.
Edit:
With the type of english you are using to produce the problem and giving no code is making it difficult to predict what the problem actually is. But I think there is something wrong with your mysqli connection file.
Add this code bit to the top of your registration.php file like this:
<?php
$connection = mysqli_connect('localhost','root','');
if(!$connection) {
die("Failed to connect" . mysqli_error($connection));
}
else {
echo "Connection Established";
}
$select_db = mysqli_select_db($connection, 'db2');
if(!$select_db) {
die("Database selection failed" . mysqli_error($connection));
}
else {
echo "Database selected";
}
?>
<?php
////rest of ur registration code goes here

Failed to connect to MySQL: Unknown database

I am working on php at the moment and I am having problem with connecting to a server data base I am using MAMP
this is my code
<?php
$con = mysqli_connect('localhost','root','root','something');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "string";
}
?>
I really hope you help me in my learning process
this is the error massage I got
Failed to connect to MySQL: Unknown database 'something'
Are you sure your password is "root"? If you're using WAMP (as suggested by localhost), the default password for mysql is "" (blank).
I think you have changed the localhost name as demo..and by default localhost username and password is root and ``(no-passsword)..So just try it..
$con = mysqli_connect('demo','root','','something');

Can't connect to database "Fatal error: Call to undefind function mysqli_connect()"

I've been scouring this site for 5 hours now trying to get this sorted, I rarely ask for help but this is one of the weirdest and most annoying things I've encountered.
First of all I'd like to say that this DID work fine, I have limited examples of what the cause is but I'll list them anyway.
Here's the full error message:
Fatal error: Call to undefind function mysqli_connect() in C:\wamp\www\game\connect.php on line 3
And here's the code
<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>
Weird thing is, it was working completely fine and just suddenly stopped, this has happened more than once.
Here's what I've tried:
Checking the extensions are enabled -
Rebooting various times -
Setting the correct php path -
Using many example codes that "work" -
I also had some code that inputted data straight from phpdesigner into the database, which successfully worked, but that no longer works and I've made literally 0 changes.
The last time it stopped working, I filled out a registration form on my site as a test (that doesn't work either) and it suddenly went off. When filling in the form I click register and nothing happens besides a refresh.
Bit extra: In my httpd file the pfp and pfpinidir are as follows
php5_module"c:/wamp/bin/php/php5.5.12/php5apache2_4.dll"
PHPIniDir "C:\wamp\bin\apache\apache2.4.9\bin\php.ini"
Your mysqli extension might not be enabled. so u need to enable that.
You have two PHP binaries on your machine. One is connected to the Apache, and is the one u use when you surf to a web page.
The phpinfo() shows you which php.ini file is used by the web php binary. u need to edit this file and in it activate the mysqli extension
You're trying to connect to DB twice, plus you're mixing MySQL APIs with mysql_error(), they do not mix together.
The 4th parameter is the DB name which is what you've done in the first example.
Use either:
<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
or omit ,"game" from mysqli_connect() - while mysqli_error() requires DB connection parameter.
<?php
// Create connection
$con=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($con));
}
?>

Cloud 9 IDE can't connect to database

I have tried a number of things to connect to my database in cloud 9 but I keep getting similar errors.
This is my PHP code:
<?php
// Create connection
$con=mysqli_connect($IP, "$C9_USER", "", "c9");
//(host,username,password,dbname)<- guide for me
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
I took the basic code from w3schools and I used it with a document explaining how cloud 9's mysql database works: https://docs.c9.io/setting_up_mysql.html
But I can't seem to connect without getting the follow error:
Failed to connect to MySQL: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
So I'm wondering if there's a different way to change the socket to the address that cloud 9 recommended: Note: MySQL socket file can be found in ~/lib/mysql/socket/mysql.sock
Credit to Loz Cherone
When using Cloud 9 IDE, the php variables $ID and $C9_USER mentioned in this article are not defined.
In order to retrieve these variables for use in your code, you must use the cloud 9 ide terminal by pressing ALT + T and entering:
echo $ID
echo $C9_USER
Then take those values and place them in a variable in your php code like so:
<?php
// Create connection
$IP = "value from terminal";
$C9_USER = "value from terminal";
$con=mysqli_connect($IP, $C9_USER, "", "c9");
//mysqli_connect(host,username,password,dbname); << guideline
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
SIDE NOTE: Make sure when running the mysql code that you have the data base turned on. You can turn it on by typing mysql-ctl start in the terminal.

PHP MySQL external database connection timed out

I'm getting a connection timed out when I try to connect to an external (not on the same server as this code is on) database through the following code. I know the settings of the database is set up right, and the login info is good, 'cause I can login fine from my computer with HeidiSQL. If anyone can see a problem with this code, thanks.
function database_connect(){
$link = #mysql_connect("xx.xxx.xxx.xx:3306","root","pass");
$sql_error = mysql_error();
if (!$link) {
echo "Connection with the database couldn't be made.<br>";
echo "$sql_error";
exit;
}
if (!#mysql_select_db("databasename")) {
echo "The database couldn't be selected.";
exit;
}
return $link;
}
database_connect();
have you tried adding the IP address of where that script is hosted to "Remote Database Access Hosts"?
If you're on cPanel, you need to allow the IP address who can remotely access the datase(mysql).
If you're not, then this is just an idea of what to do.
Problem known now.
My webhost does not allow connections to an external database.

Categories