000webhost website database connection Error - php

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

Related

Phpmyadmin in remote pc is accessible, but can't connect it with PHP PDO

I'm able to connect phymyadmin of remote pc, but when i try to connect to remote db, I'm getting connection refused error.
I have seen similar kind of question, but not yet answered also it is not active now.SQLSTATE[HY000] [2002] Connection refused with right port
<?php
$servername = "192.168.1.12";
$username = "root";
$password = "root";
try {
$conn = new \PDO("mysql:host=$servername;dbname=my_db", $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();
}
?>
In remote pc, bind address in mysql configuration file was set to 127.0.0.1 instead of 192.168.1.12

MSSQL using PDO to connect

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.

c9.io php pdo connect to mysql

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');

Connecting to a remote mysql server

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();
}
?>

mysql_connect always allows access

I am attempting to connect to a mySQL database running on my local computer using a local test server set up using XAMPP. I am just using the root account which has no password assigned. I tried it as below and everything appears to work fine. I got my message echoed out to say I had successfully connected. I then tried to force an error by entering anything else in as the host, username or password and it still echoes "connection successful".
This is the code I am using to connect:
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$myConn = mysql_connect($db_host, $db_username, $db_password) or die('Connection error: '.mysql_error());
echo 'connection successful';
?>
I was wondering whether it is me or maybe the install of apache/mySQL that I am using that is causing such an absurdity.
Something must be screwed up with your install:
marc#panic:~$ php -a
php > $x = mysql_connect('bleargh', 'foo', 'bar') or die(mysql_error());
PHP Warning: mysql_connect(): Unknown MySQL server host 'bleargh' (2) in php shell code on line 1
Unknown MySQL server host 'bleargh' (2)marc#panic:~$
About all I can think of is you've got a DNS server that returns a valid address for ALL lookups, causing all hostnames to resolve.
try this one
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$con = mysql_connect($db_host,$db_username,$db_passwd);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
echo 'connection successful';
mysql_close($con);
?>
It will always echo 'connection successful' because you aren't checking the status of the connection.
if (!$myConn) {
echo 'Sorry, there was an error '.mysql_error();
} else {
echo 'connection successful';
}
Depending on how you executed the code, you might be running into this:
If a second call is made to mysql_connect() with the same arguments,
no new link will be established, but instead, the link identifier of
the already opened link will be returned. The new_link parameter
modifies this behavior and makes mysql_connect() always open a new
link, even if mysql_connect() was called before with the same
parameters. In SQL safe mode, this parameter is ignored.
So, either do an explicit check or:
$myConn = mysql_connect($db_host, $db_username, $db_password, true) or die('Connection error: '.mysql_error());

Categories