Cloud9 and MySQL, Connection Refused - php

I'm sure this will be pretty easy, but I've been looking at this for a while, and haven't figured it out yet.
I've got a project in Cloud9 that I am trying to set up to access a MySQL database.
I've started the MySQL engine (mysql-ctl start), so I don't think that's the issue.
Here are my variables:
$db_hostname = "[username]-[projectname]-[assigned id]"; //this was retrieved by using 'SELECT ##hostname;' within MySQL
$db_database = "c9"; //default cloud9 database
$db_username = "[username]";
$db_password = ""; //the default is no password
$db_port = 3306; //the default cloud9 MySQL port
And here is my mysqli connect statement:
$conn = mysqli_connect($db_hostname, $db_username, $db_password, $db_database, $db_port) or die("Connection failed: " . mysqli_connect_error());
And this is the error that is being sent back:
Warning: mysqli_connect(): (HY000/2002): Connection refused in
/home/ubuntu/workspace/insertWorkoutScript.php on line 11
Call Stack:
0.0009 248728 1. {main}() /home/ubuntu/workspace/insertWorkoutScript.php:0
0.0012 249768 2.
mysqli_connect() /home/ubuntu/workspace/insertWorkoutScript.php:11
Connection failed: Connection refused
Line 11 (the line that the error identifies) is the $conn = mysqli ... statement. I've verified that the variables are being filled (I've got an echo for each variable name and it's value). I've double and triple checked the value I'm using for $db_hostname (which as I said, I retrieved from cloud9 using the SELECT ##hostname; statement). And as I said, I've made sure to Start the MySQL instance with the $ mysql-ctl start line in the terminal. Thoughts on what simple thing I'm missing here?
Thanks

Thank you #HPierce for the reminder to try getenv('IP'). I had tried that earlier but got a bad function response. My guess is that I typed it incorrectly, or was using it incorrectly. I've tried that again, and am now getting a good connection. So the correct settings are:
$db_hostname = getenv('IP'); // as opposed to using results from select ##hostname;
$db_database = "c9"; //default cloud9 database
$db_username = "[username]";
$db_password = ""; //the default is no password
$db_port = 3306; //the default cloud9 MySQL port
Everything else stayed the same. And I've also now solved my SQL insertion error as well.
Thanks again for your help.

Related

if statement doesnt need action

I'm making a user login system with php. Obviously to do that I need to have databases and I do using PHP MyAdmin. The connection in my php to mysql didn't work at first so I looked it up online and found this code:
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db = 'a_database';
if(!mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db($mysql_db)){
die(mysql_error());
}
?>
And this would be enough to connect to SQL. So here is my question: how can it be that you don't have to actually connect to the database in order for it to work? Because to my knowledge of php all that is doing is checking if that works, it isn't actually connecting. So is this code correct? Or do you need to add
mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db($mysql_db))
to this code to make it work? Thanks in advance.
(I'm using XAMPP btw)
mysql_connect tries to connect to to database. If it's false (connection failed) - your script dies. Otherwise - connection established succesfully and we need to check second condition
mysql_select_db selects db. It is false - your script dies too, if not - db selected successfully.

cannot connect php to postgres database using pg_connect

I just try to connect my php code to postgres database but unfortunately it is not work, here is my code :
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=public";
$credentials = "user=postgres password=postgres";
$db = pg_connect( "$host $port $dbname $credentials" ) or die('Could not connect');;
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
browser result :
Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: database "public" does not exist in /var/www/test/pgtest.php on line 11
anybody know what is wrong here ?
Are you sure the name of database is 'public'? As fas as I remember 'public' is a name of default schema inside the PostgreSQL database.
Please make sure the database 'public' really exists.
From the command line, become the postgres user, start psql, and issue the \list command to list your databases. Be sure the database you're trying to open is in the list.
Be sure your pg_hba.conf file allows the database to be opened.
Remove everything except $dbname from the pg_connect call and allow the API to use the defaults. Add parameters back as indicated by the error message(s).
sorry but finally I found that my question was wrong, public and testdb are Schemes inside postgres database,

Unable to access MySQL database

I'm trying to use MySQL with my first website, and using biz.nf to host it.
The website control panel gives me the following info for the MySQL database I have created, which I have stored in a login.php file
<?php
$db_hostname = (hostname);
$db_database = (db name);
$db_username = (username);
$db_password = '********'; //not showing here
$db_port = '3306';
?>
I then try and access it with the following code:
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_port, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
?>
Which doesn't work, giving me the following error.
Warning: mysql_connect() [function.mysql-connect]: Access denied for user '3306'#'83.125.22.189' (using password: YES) in /srv/disk12/1570263/www/erfbattle.co.nf/index.html on line 18
Unable to connect to MySQL: Access denied for user '3306'#'83.125.22.189' (using password: YES)
I tried doing a port check on fdb4.biz.nf with port number 3306, and found that the port was closed. I'm kind of new to this, and trying to figure out what could be wrong here.
You have put your parameters in the wrong order. As you'll notice in your error, the port is being passed as the username. You should change the order of your parameters.
That being said, you shouldn't use mysql_ functions, as mentioned in the comments above. Use mysqli or PDO instead.
Your statement
$db_server = mysql_connect($db_hostname, $db_port, $db_username, $db_password);
should be
$db_server = mysql_connect($db_hostname.":".$db_port, $db_username, $db_password);
Port 3306 is the default port for MySql. Try the hostname with Localhost. And in the control panel assign the Username to the database. In Cpanel there will be a option to do this.

MySQL table doesn't exist when it does exist

I have this query:
mysql_select_db('scanner');
$query = "SELECT * FROM scanner.proxy ORDER BY RAND() LIMIT 20";
$result = mysql_query($query) or die(mysql_error());
it tells me:'scanner.proxy ' doesnt exist.. even though I do have it the table with the database. It is weird, cause my other queries work..but this one doesnt.
UPADTE:
Event when I put this:
$user='root';
$user='root'
$password='<removed_password>';
$dbname = 'scanner';
$host = 'localhost';
$link = mysql_connect($host, $user, $password) or die (mysql_error());
mysql_select_db($dbname, $link) or die (mysql_error());
it gives me this...
Unknown database 'scanner'
But I can see the scanner database in the phpmyadmin
Even when I type in phpmyadmin the sql statement
SHOW TABLES FROM 'scanner'
it says it cant find scanner
We solved this problem which occurred when connecting to multiple remote MySQL databases within the same script by adding a re-connect method before every mysql_query statement. The re-connect method invoked both mysql_connect and mysql_select_db functions. We had previously tried setting the new link parameter to true in the connect statement(s) but We still got database errors and all kinds of funny behavior.
make sure the user you use to connect has enough rights to query that table. Perhaps that table was created by another user.
EDIT: Perhaps you need to grant access to scanner from a remote location. Running this sholud help on that.
GRANT ALL ON scanner.* TO your_user#'IP_ofServer_where_PHP_runs' IDENTIFIED BY 'PASSWORD';
You have set the $dbname = 'aws_backlinks', but you are trying to connect to a different database within your query. Change your connection string to include this db instead or just connect to the server and set your database at the time of the query.
$db_host = 'localhost:Port';
Specify port for localhost.
I was facing the same problem and specifying the port solved the problem.
You're missing the argument that specifies the connection (result of mysql_connect()) in your mysql_select_db() call:
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'whatever';
$db_name = 'scanner';
$link = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name, $link);

unable to connect to mySQL Db

I am a newbie to PHP and to the net world, and trying to connect to a mySQL Db using this PHP code:
<?php
echo "Hello World \n";
$mysql_host = "MySQL Host"; // this is what specified to use in mySQL management page at my host
$mysql_database = "mysql_database";
$mysql_user = "mysql_user";
$mysql_password = "mysql_password";
echo $mysql_host;
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db($mysql_database);
if (!$db_selected) {
die ('Can\'t use '.$mysql_database.' : ' . mysql_error());
}
?>
please, what can be wrong with this code? I couldn't find any syntax error and still - I am not able to connect to the Db
can it be and the problem is related to the host only?
The following variables should contain values that are respectively:
$mysql_host - IP or host name of the server with your database,
$mysql_database - name of the database,
$mysql_user - name of the database user,
$mysql_password - password for specific user to the specific database,
Make sure all of them are correct and then the problem should be resolved. If it is not, let us know.
If you are actually typing MySQL Host in your code, then you're not providing a valid hostname. You should try with localhost instead.
Otherwise, you current host must have provided you with the needed information.
You should also tell us what error your script gives you, when it dies. That enables us to give a qualified answer instead of guessing.
as #Michael says,
$mysql_host = "MySQL Host";
is definitely wrong, that field must have a either a valid host/socketname or localhost..
These guys are spot on. Also, the password is usually blank on a localhost program.
But, this is just something I noticed:
Your $conn variable is missing an 'n' in your 'if' statement.
You are storing connection in $conn but in if condition you are checking with ($con).
(There might be other errors. But I can see this error)

Categories