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 :)
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.
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
I have two servers setup on Amazon AWS. One server is running PHP and the other has MySQL. I can connect to the MySQL database through my terminal and MySQL Query Browser.
I am trying to get a connection between the PHP and MySQL servers.
Here is the PHP Code that I am using (works for "localhost" databases)
$dbbase = 'mydb';
$dbuser = 'myuser'; // Your MySQL username
$dbpass = 'mypassword'; // ...and password
$dbhost = 'localhost:3306'; // Internal IP for MYSQL Server
// connect to database
$dblink = mysql_connect($dbhost, $dbuser, $dbpass)
or die ("System Down");
mysql_select_db($dbbase, $dblink)
or die ("Database Down");
It is my understanding that I should be able to route this an internal AWS traffic, but at this point I will take anything that works and build from there.
Here is what I have done:
Added the ip of the PHP server to the Security Group for MySQL(3306) permissions
Tried to use the internal, external, and private IPs/DNSs of the MySQL Server as the $dbhost variable
Created myuser#% (wildcard) on thy MySQL server
Any ideas or tips would be much appreciated.
I had the same issue - turns out MySQL extension for PHP is NO longer included in PHP5!
"In PHP 5 (updated PHP 5.0.4), the following changes exist. Built in: DOM, LibXML, Iconv, SimpleXML, SPL and SQLite. And the following are no longer built in: MySQL and Overload."
Source: http://php.net/manual/en/install.windows.extensions.php
I've got this working.
I think the big trick was to add this rule to the Security Group:
Type: MySQL
Source: 10.0.0.0/8
My understanding is that 10.0.0.0/8 covers all internal amazon IPs. I think you could tighten this up, but it is possible for the internal IP of your servers to change, so that would need to be managed.
Then in my PHP script I used the Private DNS of my MySQL Server. It should look something like this: ip-10-10-100-100.ec2.internal:3306
In the end, I think that is everything that I did.
I have a URL from frontend access to the database.
https://db.blabla.com
Is this enough information to connect programmatically to the database? I'm trying with things like this (php):
<?php
$username = "xxx";
$password = "xxx";
$hostname = "db.blabla.com";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
And it seems at least to recognize that the host exists, but it says timed out connection.
It seems to use tcp://
I also did a random test with java and jdbc:mysql:// but same problem.
I don't have an idea what the protocol and port are supposed to be... is it possible to guess it, maybe trial and error...?
Thanks
Edit: Could file ending .do for server files give a hint for the type of database used?
Edit 2: This is not a php question, I just put php example. It's about how (if possible) generally connect.
It isn't enough information. You have no idea what the backend is doing. There might be a database, there might not be. It might be MySQL, it might not be. It might be on the same host, it might not be. It might have the same passwords as the front end, it shouldn't.
Any DBA worth his salt will have configured the database so only the host running the front end (and maybe a few other authorised hosts) can access it. Any decent network admin will have ensured that those hosts are the only ones that the firewall will allow access to the database server as well.