This might be a stupid question but why am i using local host if my site is being hosted with rackspace?
$db_host = "localhost";
$db_username = "*****";
$db_pass = "********";
$db_name = "lds";
$link = mysql_connect('localhost','*****','*********');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
It appears to be getting a connection because I'm successfully echoing 'Connected successfully'.
Maybe something is terribly wrong because i noticed when i took out localhost from both mysql_connect function and from the db_host variable... it still says connected successfully.
Would that be because i've already wired the ftp connections up in my IDE?
Any help would be greatly appreciated. Thanks in advance.
Your database server is running locally to the remote machine, where you are executing your PHP script(s). To your script the database appears to be localhost even though the web server and database server are remote relative to you.
Per the documentation for mysql_connect:
If the PHP directive mysql.default_host is undefined (default), then
the default value is 'localhost:3306'.
This would seem to indicate that if no value for the database server is provided, the default is used. Also note that the documentation also states that this function is deprecated in PHP 5.5.0.
See instead: http://php.net/mysqli and http://php.net/pdo
As long as your php we server and the MySQL database server are on the same machine localhost will work
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 try to connect my android application using JSON Parser to the web hosting. But whenever I try to connect (even just open using url in the browser) I will get the error message.
<?php
$dbHost = 'http://sql4.000webhost.com/localhost';
$dbUser = 'a6410240_cbetTD';
$dbPass = 'xxxxxx';
$dbName = 'a6410240_cbetTD';
$conn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName,$conn);
?>
This is my database.php file. The full error message is
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'http' (4) in /home/a6410240/public_html/database.php on line 8.
I have tried change the $conn but still it didn't worked for me.
Thanks
If your database and application is on same server then use "locahost" in $dbhost.
And if your database and application is on different servers then you need to use IP address or hostname in $dbhost and the database user should be added on database server provided with required privileges.
The problem you are having was already mentioned in one of the comments, this one to be precise.
For your solution to work, all you need to do is omit the part http:// at the beginning and probably /localhost at the end.
The host is only the domain you are referring to. In this case sql4.000webhost.com. With /localhost you tried to already connect to a database, although your configured database is supposed to be a6410240_cbetTD.
MySQL use TCP port 3306 by default
($dbport) and hostname or IP address ($dbhost). For LAMP (Linux-Apache-MySQL-php) you can find a lot of tutorials.
Usually MySQL server listens internal port (which can't be reached via Internet) for security purposes.
If you familiar with docker, you can simply download examples of LAMP solutions from hub.docker.com.
I'm just an amateur when it comes to PHP programming and I was hoping if you could help me in this problem of mine. I just started learning php last week and my problem is that i cant find a way on how to connect to a server computer's local host. basically the code in my global.php is this:
<?php
error_reporting(0);
session_start();
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "carlog";
mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db($dbname) or die (mysql_error());
?>
so i'm connected to the server computer via a viritual router. I was hoping that the client computer could connect to the server's local host. I tried changing the "localhost" to the server's IP add but nothing happened. the client computer wasn;t able to connect to the server (only to its own localhost)
I would be really happy if anyone of you could help me in this problem of mine. thank you
Which version of PHP are you running? This method is deprecated. I recommend you to check out how to set-up a PDO connection. http://php.net/manual/en/pdo.connections.php
Try ping in your terminal and check if you can reach it. Use this IP in your code.
Hiding all the errors isnt helping you debugging your issue.
Use:
error_reporting(E_ALL);
and check the output
First of all, if any errors are being thrown right now, the error_reporting(0); at the top is preventing the system from telling you what it's seeing.
Secondly, as others have pointed out, please try and see if you can use PDO or MySQLi instead of the mysql_connect() family, as it is indeed deprecated and scheduled for removal from PHP in the future.
Thirdly, ensure that MySQL server is running on the default port of 3306, has no firewall closing it off, is running, etc, on the remote server :)
I've made mysql.php file which contain codes to connect to my database.
However, i'm getting this error message over and over again.
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in C:\CustomerData\webspaces\webspace_00290195\wwwroot\mysql.php on line 7
Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to MySQL server on 'localhost' (10061) in C:\CustomerData\webspaces\webspace_00290195\wwwroot\mysql.php on line 8
Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\CustomerData\webspaces\webspace_00290195\wwwroot\mysql.php on line 8
Unable to select database
I've checked if my username, host, database, and password were misspelled, but they were all correct.
I've looked around the internet, but couldn't seem to find the right one for my problem.
My mysql.php file contains:
<?php
// Mysql settings
$user = "example_example";
$password = "example";
$database = "example_example";
$host = "localhost";
mysql_connect($host,$user,$password);
mysql_select_db($database) or die( "Unable to select database");
?>
I've changed my user, password and host name to "example".
I just couldn't find the solution for myself, and I need your kind help.
Thank you in advance.
Please try to find out the error by using mysql_error() function as follows :
mysql_connect($host,$user,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
and then try to find the exact reason for the failure.
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061)
here a list of questions you have to do when this error appears:
1.- is mysql running on the server?
1.1.- is mysql running on default port or in a custom port?
1.2 - is mysql accepting connexions (from localhost, from an ip...?
1.3 - your firewall is blocking mysql?
2.- the database exists on the server?
3.- the user:password have access to that database?
3.1- the user:password can log in from localhost, from an ip... from any place that mysql
is accepting?
I think I don't forget anything
Please check first that your mysql server is running properly.
Mysql settings
$user = "example_example";
$password = "example";
$database = "example_example";
$host = "localhost";
$cn=mysql_connect($host,$user,$password);
mysql_select_db($database,$cn);
Please try this it may work
I am not expert of php but this code is working perfectly in my website
if you are getting any error after applying this code then please inform me i will rty to solve the problem.
Thanks
You should provide the connection that need to use from database whle selecting database. Also use single quote instead of double quotes.
<?php
// Mysql settings
$user = 'example_example';
$password = 'example';
$database = 'example_example';
$host = 'localhost';
$link = mysql_connect($host,$user,$password);
mysql_select_db($database,$link) or die( "Unable to select database");
?>
Reference
You have to get the database information first. You posted this problem when you try to connect to your database in your website hosting, right?
So, go to your hosting control panel to get the MySQL database (and take note of where the database is stored on! Sometimes some hosting providers don't use "localhost". Instead, some of them may use 'serverXX.yourhostingprovider.tld' where XX means the server ID), and then change the corresponding variables with the value you've obtained from the hosting control panel.
We are using the below script.
Actually our DB is in one server (say www.server1.com) and the PHP file
containing the below connection string is in another server (say
www.server2.com)
If we place the PHP file containing the below script in the same server
where the DB exists that is www.server1.com/dbconnection.php it is working
fine.
But if we place the PHP file containing the below script in another server
www.server2.com/dbconnection.php it is not working. This displays the
error 'Something went wrong while connecting to MSSQL' Please advise.
Also to handle error if we use 'die('MSSQL error: ' .
mssql_get_last_message());' nothing displays just an empty page.
Please advise how to handle errors.
Script (dbconnection.php)
$server = 'xxx.xxx.xxx.x,xxxx'; //IP, Port
// Connect to MSSQL
$link = mssql_connect($server, 'username', 'password');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
// Connect to DB
$db=mssql_select_db("databasename",$link) or die("Unable to select
database ");
Thank You
How to get error message, I do not know, but if you are migrating your PHP app from windows server to linux (FTP servers are more usual on linux), you can try change:
$server = 'xxx.xxx.xxx.x,xxxx'; //IP, Port
to
$server = 'xxx.xxx.xxx.x:xxxx'; //IP: Port
As described in first parameter of manual: http://www.php.net/manual/en/function.mssql-connect.php