I am new to php and I know this is a really stupid question to ask and I can be thrown out of the stack overflow community for this, but I'll ask it anyway.
Normally when we connect to localhost we write
if(!mysql_connect('localhost',$username,$password) || !mysql_select_db('user_transaction'))
{
echo 'Cannot process the requested operation';
}
else
{
// other code..
}
*But I want to connect to a domain in place of localhost like "http://myownsite.com" . *Can anyone please tell me as how can I achieve this?**
To connect to remote database you just have to replace "localhost" with your remote server's IP/hostname.
For example
mysql_connect('myownsite.com',$username,$password)
Three important things:
You provide an IP address od hostname without any protocol like "http" etc.
Your remote server needs to be configured to accept incoming connections on MySQL's port (iptables usually).
You remote MySQL server also needs to be configured to accept remote connections (my.ini).
P.S.
mysql extention is deprecated for long time. You should not use it at all. Use mysqli or PDO instead.
It's important to know where the mySQL is running on your remote host... for example, the most common way to connect to a mySQL database is using mysql.yoursitename.com. So your code would look like this:
mysql_connect('mysql.myownsite.com',$username,$password)
If you have your mySQL running on some other subdomain, just adjust as needed. But the above would be the most common way.
just use :
mysql_connect('myownsite.com',$username,$password)
Related
I have found glimpses of the fact that it is possible to connect PHP to MySQL via pipes in their documentation of MySQLi, but I cannot, for the life of me, find anyone explaining what is needed.
The host parameter claims:
When possible, pipes will be used instead of the TCP/IP protocol.
But when is it "possible"? I have my own machine, and I definitely have the necessary privileges to achieve this, I just don't know how. Connecting to the host localhost reports "Localhost via UNIX socket" when examining the host_info.
Trying to follow one (downvoted) comment from that page, and connecting to host ., with socket parameter set to mysql, causes a 2002 connection error.
How do I tell it to (always) connect via a pipe instead?
Today I had the same issue and it required much time to solve this on Windows.
I can establish a named pipe connection with the following code:
$connection = new mysqli('.', 'user', 'pass', 'database', null, '\\\\.\\pipe\\name_of_pipe');
The server, where I want to connect to, has the following configuration:
[mysqld]
skip-networking
enable-named-pipe
socket=name_of_pipe
Using '127.0.0.1', 'localhost' or NULL as hostname doesn't work. Also you must specify a path to the named pipe and not just the name of the pipe.
Unfortunately the PHP documentation is a little bit weak here...
Named Pipes only work under Windows.
Also
Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.
It is not mentioned in the actual PHP documentation, but it should be still valid.
$con=mysqli_connect('localhost:3306','Punya','password','saain');
My PHP website, which is live is unable to connect to database. It works fine on my Pcs local server. When I write localhost:3306, it executes the echo statement I wrote for debugging.
If I write just localhost, it gives:
http 500 error
.
Cannot say much without knowing the actual error. But I could assume a few things based on what you have given.
When you pass localhost as the hostname to the mysqli_connect() function, it tries to create the connection using a Unix socket instead of TCP/IP. So it seems like either you don't have permissions to access the socket, or you need to get the correct socket information from your hosting provider.
Also, when you pass it as localhost:3306, you force it to connect via TCP/IP instead of socket. That's why it works.
However, you can force mysqli_connect() function to connect via TCP/IP by providing the hostname as 127.0.0.1.
$con = mysqli_connect('127.0.0.1','Punya','password','saain');
If this doesn't work, I assume they have defined a different default port for mysql in the php configuration. You can see it by printing it like:
echo ini_get("mysqli.default_port");
In case this default port is different from 3306 (which is unlikely), you can pass the port as the fifth paramter to mysqli_connect() function like this.
$con = mysqli_connect('127.0.0.1','Punya','password','saain', 3306);
You need to contact your hosting provider to get more details on this. Until you do that, your solution is to connect as shown above.
I hope you understand why it happens :) Feel free to ask if you have any doubts.
Try to check in
check here
Synax errors
check in your 👇
file.php
Check what version your live server is running and what version our location machine is running. most of the time environmental factors make these types of errors. I'm going to assume your hosting environment is a shared hosting for this answer
There are couple of things you can do identify the problem first.
Enable Errors
if you are hosted on a cPanel shared hosting server I doubt you can change the php.ini file. what you can do is enable PHP errors on runtime so you will have a proper error than just a 500 error. put the below codes in the top of your PHP file
<?php
error_reporting(E_ALL);
ini_set("display_errors", "On");
PHP info file
create a php.info file to identify what sort environment you are hosting your website on.
<?php
phpinfo();
?>
Cpanel has there own MYSQL server domain and port. this information you should get when you are creating a database in cPanel.
Once you have the error. paste it here we might be able to give you a better answer how to fix it
When using PHP to connect to a MySQL database on a web host, what is the best way to refer to the server?
The MySQL admin page on my web host says what the IP address of the server is, but can I use something else other than the IP number?
Eg.
$con = mysql_connect("l00.50.10.10","user","password");
if (!$con) { die('Could not connect: ' . mysql_error()); }
When I use localhost I get this output:
Could not connect: Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2)
You can refer to the script's system with "localhost" as the host.
$con = mysql_connect("localhost", "user", "password");
However, connecting to the IP is most likely failing because you have a lowercase L where you should have a one digit (but I'm hoping the IP in the question was just an example).
Also, use mysqli or PDO instead of mysql, which is on the verge of deprecation.
You can the database server's host name, e.g. mydbbox.example.com. Using the IP address may be faster, as no DNS lookup will be necessary.
Also see this SO question.
The answer to this question depends on how your host (or their control panel) configures MySQL privileges. The privileges are IP or hostname specific, so often connecting by IP or by hostname may not have the same connection results. If in doubt, I would stick to exactly the settings they give you.
Read more: http://dev.mysql.com/doc/refman/5.0/en/account-names.html
If database host is same computer - use localhost. MySQL will connect by local socket, not tcpip (network) when you use localhost with port number or not.
Sometimes server does not support socket connections, and you will get error like this:
Could not connect: Can't connect to local MySQL server through socket ...
EDIT/CORRECTED:
If you cant connect by socket - use 127.0.0.1. It will force conncetion by tcpip.
If database server is installed on other machine - you can use IP address or domain.
IP address is better in most cases, because connection can be established even when DNS (domain name server) is not working correctly or when domain expired.
And one advice - use mysqli_ instead of mysql_ functions in new application. New functions are very similar in use, but more secure.
Old mysql_ functions are obsolete and they are not recommended for new applications. They are still available in PHP, because of compatibility with old, outdated software (CMS systems, e-commerce systems etc
PHP Help says (about mysql_connect):
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL
extension should be used.
More about connecting with localhost - info from MySQL Reference
On Unix, MySQL programs treat the host name localhost specially, in a
way that is likely different from what you expect compared to other
network-based programs. For connections to localhost, MySQL programs
attempt to connect to the local server by using a Unix socket file.
This occurs even if a --port or -P option is given to specify a port
number. To ensure that the client makes a TCP/IP connection to the
local server, use --host or -h to specify a host name value of
127.0.0.1
Note, that "--port" and "-P" options are decribed in other context than PHP, but connection mechanism works similar and localhost is "special" name, doesnt matter is this PHP, console or some other software.
Have you tried to type in google something like this?
php Could not connect: Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (2)
This depends mostly on how your host has things configured. From my experience you will use "localhost", the server's IP address or a subdomain of some sort set up by the host.
I'd start by testing "localhost" as this is the default for most shared hosting environments.
$con = mysql_connect("localhost", "username", "password");
I get this everytime I use mysql_connect() no matter what database I choose:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'IP' (111) in filename.php on line 17
A MySQL error has occurred: Can't connect to MySQL server on 'IP' (111)
The exact same file works on my personal website fine. I have tried multiple databases hosted on different servers and it always gives that output.
The database itself is hosted on the same server, but using its full IP in mysql_connect(). Using localhost:port doesn't work either as it says:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in filename.php on line 17
A MySQL error has occurred: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
But using the IP should work as it has worked calling it via the same file hosted on other servers.
This is the code:
$connect = mysql_connect($db_url,$db_user,$db_pass); // connects
if ($connect == false) exit("A MySQL error has occurred: " . mysql_error());
Now since the file works on other servers i am guessing it is something to do with the server it is on and might need something changed. I don't personally have root access to the server (just my part of the shared host). Is there anything I can do i php, editing the php.ini file or something I should pass on to someone with root access?
Edit: Ok it turns out that the server doesn't have access to outside databases, so thats why the IP didn't work. Thanks for all your answers but we have decided simply to change hosting provider. We need to be able to access an outside database.
This is on a hosting service? Check their documentation, there will be something that tells you where to find mysql. It isn't necessarily localhost.
For example, on startlogic.com, you use: yourdomain.startlogicmysql.com
Can you connect using mysqladmin using the same host, username and password?
mysqladmin -h $db_url -u $db_user -p $db_pass
Replace $db_xxxx with real values.
If that works from the same host as your php script, then sudo to the apache User and try the same test. One of those must be failing.
EDIT: nevermind on sudo part, I noticed that you don't have root access.
Something else to try: Use '127.0.0.1' instead of 'localhost'. I have had issues before where mysql stupidly assumed it could silently change 'localhost:' to '/var/run/mysqld/mysqld.sock'.
Your wording is not very clear, I hope you are not thinking you can connect to the same mysql server from any old web server just because you know the IP address and port number. If the web host is at all competent, they have probably firewalled mysql so it is only accessible through their own web servers.
Ok, If you can answer this question, you deserve the nobel peace prize. Anyways, here's the situation.
I'm using Slicehost dot net, and I have 2 slices (two different IPs). One slice is my mail server and the other is my normal website. I'm running Ubuntu (8.04 or 8.10, something like that, it shouldn't matter). What I'm trying to do is access the MySQL database on the Mail server from the other slice. I'm trying to do that with PHP. I really appreciate any help.
mysql_connect()
$resource = mysql_connect('other-server-address.com', 'username', 'password');
The first parameter is the mysql server address.
Server Param
The MySQL server. It can also include
a port number. e.g. "hostname:port" or
a path to a local socket e.g.
":/path/to/socket" for the localhost.
If the PHP directive
mysql.default_host is undefined
(default), then the default value is
'localhost:3306'. In SQL safe mode,
this parameter is ignored and value
'localhost:3306' is always used.
Unless I'm misunderstanding... this setup is pretty common. Any trouble you're having might be related to the following:
Firewall settings
Grant access to the mysql user to connect from the other host
my.ini settings not allowing outside connections
Some other related SO questions:
Connecting to MySQL from other machines
How do I enable external access to MySQL Server?
php access to remote database
How to make mysql accept connections externally
Remote mysql connection
Assuming your mail server is at IP 192.168.1.20 and web server is 192.168.1.30
First of all you need to allow the web server to access the mysql database on your Mail server .
On 192.168.1.20 you run the mysql command and grant access on the database needed to your web server
mysql> grant all on mydb.* to 'someuser'#'192.168.1.30' identified by 'secretpass;
Your PHP code connects to that database with something like:
$conn = mysql_connect('192.168.1.20', 'someuser', 'secretpass');
mysql_connect() returns a link identifier if the connection is successful. Also you have to do is keep the references to both links.
When you want to use which ever link, simply include the link as an argument.
$link1 = mysql_connect($host1, $username1, $password1);
$link2 = mysql_connect($host2, $username2, $password2);
$r = mysql_query(QUERY, $link1);
Simple as that.