I am trying to connect mysql database using php. But it is running fine on local machine but not when hosted online and error appeared as mysqli_connect(): (HY000/2002): Co
If your website and MySQL database are hosted on separate servers, make sure that you've mentioned correct MySQL hostname (resolvable) in connection string.
In case you've specified the correct username, most probably your MySQL server isn't allowing remote MySQL connections from web server where you site resides. Contact your web hosting provider for the exact MySQL hostname and to allow your web server IP address for remote MySQL connections.