PHP cannot connect to MySql database on live web server - php

$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

Related

How to connect a PHP app to MySQL via pipes?

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.

Cannot connect to mysqli. Error no 2005 [duplicate]

I was using mysql 5.6.11,it usually turned down and show me this:
2005 - Unknown MySQL server host 'localhost'(11001).
Currently my resolution is to turn off the network,than it return to normal.I had searched a lot,but no answer is revalent to it.So,does anyone knows the reason?
ERROR 2005 (HY000): Unknown MySQL server host 'localhost' (0)
modify list of host names for your system:
C:\Windows\System32\drivers\etc\hosts
Make sure that you have the following entry:
127.0.0.1 localhost
In my case that entry was 0.0.0.0 localhost which caussed all problem
(you may need to change modify permission to modify this file)
This performs DNS resolution of host “localhost” to the IP address 127.0.0.1.
I have passed through that error today and did everything described above but didn't work for me. So I decided to view the core problem and logged onto the MySQL root folder in Windows 7 and did this solution:
Go to folder:
C:\AppServ\MySQL
Right click and Run as Administrator these files:
mysql_servicefix.bat
mysql_serviceinstall.bat
mysql_servicestart.bat
Then close the entire explorer window and reopen it or clear cache then login to phpMyAdmin again.
The case is like :
mysql connects will localhost when network is not up.
mysql cannot connect when network is up.
You can try the following steps to diagnose and resolve the issue (my guess is that some other service is blocking port on which mysql is hosted):
Disconnect the network.
Stop mysql service (if windows, try from services.msc window)
Connect to network.
Try to start the mysql and see if it starts correctly.
Check for system logs anyways to be sure that there is no error in starting mysql service.
If all goes well try connecting.
If fails, try to do a telnet localhost 3306 and see what output it shows.
Try changing the port on which mysql is hosted, default 3306, you can change to some other port which is ununsed.
This should ideally resolve the issue you are facing.
Follow these steps to fix this error
Use \connect root#127.0.0.1 instead of \connect root#localhost
if it doesn't work then go to C:\Windows\System32\drivers\etc\hosts and check the IP address attached to host name.
use that IP, so it will be.
\connect root#the_ip_address_you_found

PHP ftp_connect() returns false always except with first used ftp server..!

The problem is ..
I just used ftp_connect() for the first time on my server
<?php
$ftp_connect = ftp_connect('ftp.server.com') or die('<b>FTP Connection Error ...</b>');
$login_result = ftp_login($ftp_connect, 'ftp_username','ftp_password') or die('<b>FTP Login Error ...Check Your UserName and Password</b>');
ftp_pasv($ftp_connect,true);
?>
It worked fine and returned true.Every thing works fine with that ftp server (ftp.server.com)
Then next time I used the same server and everything was fine...BUT
when I tried with another server (say: ftp.server2.com) it returned false,i.e host not found ..!!
I tried with all my friends ftp servers and nothing could connect except that one(ftp.server.com)
what's actually going on ... why is this happening and how can I solve this ... right now i can't connect any ftp server (except my ftp.server.com) ....
Thanx in advance ...
And The above thing works fine on my pc (IIS,PHP 5.3.8)
Updated :
ahhh ....... figured out the problem .... but didn't find a solution.!!!!
the server (here: ftp.server.com) has the same IP which the host server, where the above php is hosted - has.... and soo the ftp_connect() worked on it....
reason is both the servers ftp and http host are from same web hosting provider...
Now its clear, (I think) that firewall is the main reason for all the problem .... how to fix that firewall...?? plz help.... Thanx again..
You are not specifying a port number. PHP's ftp_connect() will use 21 as the default port number if you don't specify one. This script may have worked for the first server because it was running on port 21, but the other servers may be failing because they're running on different ports. Ask your friend(s) what port they're using, or use something like nmap to find it yourself.

mysql_connect doesn't work on a certain host

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.

How do I access another MySQL Database from another IP Address with PHP?

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.

Categories