I am attempting to connect to a mysql database using the c9.io development environment. I have followed their documentation and have seen multiple links, 1, 2 and 3.
I verified the mysql service is running. I also verified the PDO extension was installed via phpinfo(). Here is my current code:
$ip = getenv("REMOTE_ADDR");
$port = '3306';
$user = "username";
$db = "c9";
try{
$con = new PDO("mysql:host=$ip;port=$port;dbname=$db;charset=utf8",$user,"");
}
catch(Exception $e){
echo $e->getMessage();
}
I get the error Can't connect to MySQL server on '10.240.x.x' (111)
If i try localhost as host, I get the error Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I also followed a comment from the second link above: echo $IP in the terminal which returns 0.0.0.0
Any assistance appreciated.
You were on the right track. On https://docs.c9.io/setting_up_mysql.html it says use $IP for host. You can use getenv("IP") instead or use its value: 0.0.0.0. That should work.
Please try something like:
$dbname = 'c9';
$ip = getenv('IP');
$user = getenv('C9_USER');
mysql_connect($ip, $user, '') or die('Could not connect to mysql');
Related
I am much confuse about 000webhost database uploading I have set my database at their and accessing it with username and password perfectly. Now trying to connect with through PHP code but i am failed, getting 500 error code and no details of the error although I have also logged exception message in my connection code nuto it only showing "A Connection error has occurred" and nothing more:
//DB Configuration
$hostName = 'localhost';
$dbUserName = 'id143984_jw_kioskuser';
$dbPassword = '***********';
$databaseName = 'id143984_jw_kiosk';
try{
$dbHandler = new PDO('mysql:host='. $hostName . ';dbname='. $databaseName, $dbUserName, $dbPassword);
}
catch(PDOException $ex){
echo '<h1>A Connection error has occurred.</h1><pre>', $e->getMessage() ,'</pre>';
echo $e->getMessage();
}
My current DB interface at 000webhost saying me that "Use localhost as connection hostname"
while in another post of 000WEbhost saying "Never use 'localhost' as your MySQL hostname! You can find your MySQL hostname by logging on to members area, entering control panel and clicking on MySQL icon."
HOw do i make successfull connection?
Finally i managed to get the answer through mysql query thanks to Show MySQL host via SQL Command Answer at stack
I run below query and got the host name and use it in my php it worked now.
select ##hostname;
show variables where Variable_name like '%host%';
$hostName = 'mysql6.000webhosting.com';
$dbUserName = 'id143984_jw_kioskuser';
$dbPassword = '***********';
$databaseName = 'id143984_jw_kiosk';
$con = mysql_connect($hostName,$dbUserName,$dbPassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}`enter code here`
mysql_select_db("$databaseName",$con);
this should give you the real reason and note the correct host name
I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$username", "$password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.
if (!empty($_POST)) {
$host = $_POST['host'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$db = $_POST['db'];
echo '<pre>';
print_r($_POST);
$con = #mysqli_connect($host . ":3360", $user, $pass, $db);
// $con=#mysqli_connect('192.168.100.42','root','vertrigo','skates');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo 'connected';
}
#mysqli_close($con);
}
I'm using this script to access remote db server in local(basically i want to access local db server through live webiste), howewer, I'm getting an error:
Failed to connect to MySQL: Unknown MySQL server host
'192.168.100.42:3360' (11004)
Please help to get out from this.
192.168.100.42 looks like a local IP. You'd need to try to connect to your static public IP and then forward port 3306 in your router to the 192.168.100.42 machine.
That's not your only problem;
Just because $_POST is not empty, don't assume it has those array keys set and not empty. Syntax like
if(isset($_POST['user'] && !empty($_POST['user'])))
Is much better.
Also, never use $_POST on any database interaction without sanitisation, and never connect to a database with the root user.
I don't know your exact application, but it's impossibly insecure.
I'm having trouble connecting to a MySQL database on my host OS (Win 7) from my Oracle VirtualBox running Ubuntu. I wrote this php code in order to connect but I keep receiving an error connecting:
<?php
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'sdb_user';
$db_password = 'password';
try {
$db = new PDO($dsn, $db_username, $db_password);
}catch (PDOException $e) {
$error_message = $e->getMessage();
include('../errors/database_error.php');
exit();
}
$query = 'SELECT * FROM books';
$books = $db->query($query);
foreach ($books as $book) {
echo $book['title'];
}
?>
I know the code works, because I can get it to run from my Windows 7 host OS using localhost instead of the IP address, and I know that both Linux on VirtualBox and Windows 7 are on the same subnet because I can successfully ping the other from each side.
I'm fairly certain that I opened port 3306 correctly on the windows side also, but I have no idea how to check if that's the problem or not.
What else should I be doing to get the code to connect to the MySQL db?
EDIT: The problem was that the port was not open for remote connections. After I opened the port I got the error message stating I was not allowed to connect to the sql server. From there I created a new user on the Windows MySQL with the username "home" and password "home" and was able to connect to it using:
$ mysql -h 192.168.1.9 -u home -p
However, when I run my file, which now reads:
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'home';
$db_password = 'home';
I still get the 'database error'
Any suggestions?
How would I connect to the demo phpmyadmin server in php? My code looks like this.
<?php
$host = 'http://demo.phpmyadmin.net/STABLE/';
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
but I get this as my error
QLSTATE[HY000] [2005] Unknown MySQL server host 'http://www.demo.phpmyadmin.net/STABLE/' (1)
You seem to be confusing two things:
the demo phpMyAdmin front-end that is backed by a db server and db/schema
the db server and schema itself
PDO needs the latter, the db server itself.
Inspecting the front-end code of the demo, I don't see anything in there that would give us the actual connection details for the db server. And that's as I would expect: I find it hard to believe that the makers/maintainers of the phpMyAdmin demo would make their actual db server available for public remote connections.
change your hostname from
$host = 'http://demo.phpmyadmin.net/STABLE/';
to your original remote hostname like eg $host = 'ukld.db.5510597.hostedresource.com';
MySQL does not work on HTTP
<?php
$host = 'demo.phpmyadmin.net';
// High chances that this is NOT your mysql hostname.
// It will not even by like /STABLE/ as you mentioned it.
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>