Trouble with basic database connection with PHP - php

I am getting familiar with WAMP and struggling with very basics. I created a test website and test database, trying to connect db to the site.
Yet I get constantly error instead of connection, error code not telling what's it all about even if site PHP settings are set to "display errors = yes". I have checked creditials dozen of times, they are correct.
Below is the simple code I made, could you please help me out what's wrong with it? Creditials are from sites mySQL manager, changed so that I can't be indentified but in similar shape.
Should the host be something else than a string? Is the PHP script somehow obsoleted/wrong or does this has something do with the fact that I am playing with free and slow web server host?
My code:
<DOCTYPE! html>
<html>
<body>
<h1>Working title</h1>
<?php
$host = "sql123.epizy.com";
$username = "epiz_12345678";
$password = "nottherealpassword";
$dbname = "epiz_12345678_NameofDatabase";
$link = mysqli_connect($host, $username, $password, $dbname);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
</body>
</html>
Thank you for your help!

WAMP is a localhost tool. here you have $host = "sql123.epizy.com"; as database host.
Solution1:
Creating an api [where you can send queries there] in epizy.com webhost to communicate between your server and the database server.
Solution2:
Simply use your own database on WAMP server.

Looking at their support page, it does say:
"Make sure you are connecting from within hosting account.
InfinityFree databases are only accessible from within your InfinityFree hosting acounts. Our database servers are not accessible from other locations, like developer tools on your own computer, websites on other hosting providers, game software, mobile apps and so on. Remote database access is only available with premium hosting."
See here: https://support.infinityfree.net/mysql/common-mysql-errors/

Related

How to access database?

I need to access the database. I've downloaded the file where database credential reserved.
The file is mysql.php
$conn = mysql_connect('localhost','user','password') or die("Connection error: " . mysql_error());
mysql_select_db('newadv',$conn) or die("Connection error: " . mysql_error());
IN the server how can I access the localhost and database.
The mysql.php file now is in server and the website is running.
I need to access the database from Mysql interface. How can I achieve this?
I finally got reply from webhosting service. They gave me the credentials to log in.
Thank you every one.
When you want to connect with server.If database present on same server then use host as localhost & database server username & password.If they are on different server then in place of localhost provide the server host details.
like:
$conn = mysql_connect('111.255.255.254','root','root') or die("Connection error: " . mysql_error());
mysql_select_db('newadv',$conn) or die("Connection error: " . mysql_error());

connect mysql database in openshift from another apllication/hosting

I have two application in one domain
1) In java and having phpmyadmin/mysql database.
2) In php5.3
I am try to connect first application database using second application.
but its not possible.
return error.
Failed to connect to MySQL: Can't connect to MySQL server on '127.13.153.130' (113)
here is my code to connect phpmyadmin database.
<?php
// Create connection
$con=mysqli_connect("127.13.153.130","adminz6RCQ***","zs4-EbW-****","testjaphp");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo "connected";
}
?>
Help me if I can do this.
thanks.
Your Java Application needs to be a scalable app so your mysql database is put on its own gear. Once its scalable it will have an externally accessible address that you can use to connect to. Simply clone your app to scalable app see here and then check your environment variables to see what your new address is.

Using IP address instead of 'localhost' as hostname in mysqli connection

I am perhaps being a bit overly cautious asking this and apologies if it has been but I want to be as secure as possible.
Is it just as secure to use my hosting accounts IP address instead of localhost when I connect to the database via my mysqli connection below? Would the speed of connection be dramatically affected at all?
$hostname = "localhost";
$database = "wwwcapco_crm";
$username = "wwwcapco_user";
$password = "cKUsaf#&^0";
$connect = new MySQLi($hostname, $username, $password, $database);
if ($connect->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli- >connect_errno . ") " . $mysqli->connect_error; }
The main reason I ask is because at the moment I am exporting my live website database and importing it into my local website testing enviroment (xampp). This takes ages and I just thought it may be a better idea to connect directly to the live database.
Thanks
Is it just as secure to use my hosting accounts IP address instead of localhost when I connect to the database via my mysqli connection below?
No. See the documentation:
All other information is transferred as text, and can be read by anyone who is able to watch the connection. If the connection between the client and the server goes through an untrusted network
Would the speed of connection be dramatically affected at all?
Since you are getting the data from a computer at a remote site instead of locally — yes, it will.
local website testing enviroment (xampp). This takes ages and I just thought it may be a better idea to connect directly to the live database
Risking your live data by exposing it to your test code is a very bad idea.

Can't connect to Dreamhost's database

at first sorry for my english, i've a important question to resolve.
I'm trying to connect to my dreamhost's database but it's returninng the following error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I've contacted Dreamhost support and they told me that
it looks like the error was the result of using a newly created database
hostname.
I see that the hostname is now working properly
Now, I have a php class to connect to my database.
this is my command to connect:
$data = new MysqlClass('mysql.test.masterweblab.com','user','pass','data_name');
This works on my localhost. Anyone have a solution?
maybe they are using a different version of php or mysql than on your localhost?
try connecting to it like this on a new page and see if this works.
$link = mysql_connect("mysql.test.masterweblab.com", "user", "pass");
mysql_select_db("data_name") or die(mysql_error());
if ($link){
echo "it worked!";
}else{
echo "Failed " . mysql_error() ;
}
mysql_close();
If that doesn't work try pinging the host from a command prompt in windows. Copy the ip address and try.
$link = mysql_connect("THE_IP_YOU_COPIED", "USER", "PASS");
mysql_select_db("data_name")or die(mysql_error());
if ($link){
echo "it worked!"
}else{
echo "Failed " . mysql_error() ;
}
mysql_close();
Let us know what that produces.
if you cant get the ip from pinging the domain there's your problem! Log into cpanel and get your sql servers ip from phpmyadmin.

Connecting to MySQL Database with Code Hosted Locally

When I try to run working php code on my localhost instead of the web server, I am getting a connection error.
Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] Connection refused
Any idea how to get the MySQL username, db, and password to work from my local machine? I am using OS X Mountain Lion and Apache.
Do I have to login to the database server and add my IP?
Thanks!
Unless you've changed the default password root is allowed to connect to localhost, so that would be something like this :
<?php
$mysqli = new mysqli("localhost", "root", "", "");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
You can replace localhost by an ip, depends on how your database is configured to allow connections, using localhost or an explicit ip.
in case you have downloaded the code from a server and are trying to make a local replica then you need to update the connection parameters to match your local configuration. You need to update username, password , database etc. as per your local settings.. Hostname you may keep as localhost
Respond back if things are not working and add more detail on how things started like how you set up the code locally..and what things you have tried yet..

Categories