so I've recently finished a course on web development, and completed a final project that functioned on my personal Apache Web Server and MySQL Database (MariaDB) through XAMPP. I should note that the site works perfectly as expected on the Apache. I'm interested in having this site hosted so that others can access it as well. (I've never had any sites hosted before, even static ones, and I'd like to do this to experiment). I've tried searching the web for hosting sites with PHP connected to SQL databases and could not find a solution. What I'd like to do is set up a site where the PHP is able to communicate with the SQL db and update realtime so that all visitors can see the changes.
From what I've gathered, I believe that I'll have to host the SQL db from a site that hosts them and separately have another site host my php site. I'm assuming that in my Database Adaptor function, I'll have to change the value of "host" in $db to connect to the SQL db being hosted rather than direct it "home". Is this correct? or am I going about this completely wrong?
This is the database adaptor function I used:
class DatabaseAdaptor {
private $DB;
public function __construct() {
$db = 'mysql:dbname=planner;host=127.0.0.1;charset=utf8';
$user = 'root';
$password = '';
try {
$this->DB = new PDO ( $db, $user, $password );
$this->DB->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo ('Error establishing Connection');
exit ();
}
}
If I'm going about this completely wrong, could you please lead me in the right direction. I've read a little about WordPress and it seems like they have some compatibility with php and databases, are they what I'm looking for?
In addition, what sites would you recommend for hosting php sites or sql databases?
You are right, you have to change this:
$db = 'mysql:dbname=planner;host=127.0.0.1;charset=utf8';
$user = 'root';
$password = '';
to the one given by your hosting company. There are various hosting companies that provide PHP and MySql hosting.
When you subscribe to a Hosting Provider, they usually have a knowledge base or guidelines on how you would setup your website through the Control Panel. From there, you will be able to setup database(s), (S)FTP to upload your PHP files to your (public) directory, and even setup email(s). There are a lot of (PHP) hosting providers it's up to you which to choose. Here are a few:
DreamHost
SiteGround
HostGator
Linode
GoDaddy
Or you might want to try a free (sandbox) from Heroku?
Happy hunting!
Related
I was wondering if I may get some help with trying to connect to an external database for one of my PHP config files. This PHP file is from a plugin I downloaded from cartmega, which is supposed to tie-in with our ticketing system called osTicket. When we connect to our DB, data is supposed to be fetched from one of the tables via a protected function and a few commands that were already on the config file.
Now I'm able to connect to my local database, however, I'm only able to connect to that local DB using localhost. I've tried using the hostname of the local server, but for some reason it wasn't pulling the data from my mariadb table structure. I need both the local and external databases because both DB's have different data that needs to populate on our ticketing system.
This is how the connection code is currently setup
protected $parent
private $_db
public function_construct($parent) {
$this->parent = $parent;
$this->_db = mysqli_connect("localhost", "username", "password", "osticket_db")
Now my thinking is that if I want to connect to an external db, then I should do the following
protected $parent
private $_db
private $_db2
public function_construct($parent) {
$this->parent = $parent;
$this->_db = mysqli_connect("localhost", "username", "password", "osticket_db")
$this->_db2 = mysqli_connect("name.name.com", "username", "password", "snipeIT")
So thanks for your guidance everyone. It gave me an idea for what to look for, so I managed to figure it out. I had to grant access to the sql server by granting access to the sql user that is trying to log into the external server. After that I edited the sql .cnf file and added a bind address, once I completed that I tried to allow the ports again, and then ran a few iptables command to allow the IP and port that is trying to access the localserver and external server. After that I was able to connect to the external database from the local database.
Thanks again!
You need to put IP of the external database instead of localhost and you need to allow remote access from the outside database to grant the access. Remote access can be accessed from cPanel.
I've read the similar questions and I tried what they said, but I couldn't get my App Engine app to connect to my Cloud SQL instance.
Here's what I've found is supposed to be correct:
index.php
$servername = null;
$username = "root";
$password = "rootPassword";
$database = "NewApp";
$port = null;
$socket = "/cloudsql/newapp-edc:us-central1:newapp0";
$connection = new mysqli($servername,
$username,
$password,
$database,
$port,
$socket);
app.yaml
runtime: php72
entrypoint: serve index.php
env_variables:
# Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
# values obtained when configuring your Cloud SQL instance.
MYSQL_USER: root
MYSQL_PASSWORD: rootPassword
MYSQL_DSN: mysql:dbname=NewApp;unix_socket=/cloudsql/newapp-edc:us-central1:newapp0
# Use the connection name obtained when configuring your Cloud SQL instance.
beta_settings:
cloud_sql_instances: "newapp-edc:us-central1:newapp0"
I've gone through all of the similar questions and answers and all of the relevant Google Docs and I can't find the answer to this.
The current method results in "Connection refused," which means, I think, that I have the socket correct as before it said, "No such file or directory." Everything seems to be correct and I have permission for all apps in the same project, and the App Engine app and the Cloud SQL instance are in the same project, so that shouldn't be the issue.
As mentioned here in this similar post, if you would like to connect your Google App Engine to Cloud SQL you will need to enable Cloud SQL proxy since Google App Engine connects from inside of Google servers to your Cloud SQL instance via proxy.
If you would not like to use Cloud SQL proxy, you will need to to authorize your IP from your Cloud SQL instance, as shown here.
I hope this helps.
Well, I ended up getting it to work by deleting that instance and creating a new one. The steps I posted were the same. My only guess to why it didn't work before is perhaps when I created a private ip address for the last one, the system disabled connecting via the unix socket. I can't think of anything else that could have created that issue.
In the end, it did work like I thought it should. I didn't need to use a proxy or go through extra steps to give permissions to the app. So, I had it right, just for some reason it wasn't working. Deleting the old SQL instance and creating a new one worked, however.
I am moving my application from Mysql extension to PHP PDO.
I am facing a strange problem.
In my development environment, I have both db server [MySQL] and web server are in single system where as in testing environment web and db servers are in different system.
The following test code runs perfectly in dev environment and fails in test environment.
require "class.DB.php" ;
class DbMaster extends DB
{
public function __construct()
{
$this->Host = "192.168.1.00";
$this->Database = 'test_database';
$this->User = "root";
$this->Password = "12345";
}
}
// Creates the instance
$db = new DbMaster();
$table = mysql_real_escape_string('persons');
$result_array = $db->query("SELECT Id, Age FROM $table WHERE Id >= :Id", array("Id" => 1));
foreach ($result_array as $rec)
{
echo '<br>'.$rec["Id"].' -> '.$rec['Age'];
}
In dev, mysql_real_escape_string should fail, because there is no mysql_connect().
But, mysql_real_escape_string works when there is a mysql server running locally. To test this in dev environment I stopped the local mysql and connected to remote database. Then I got the following error:
Warning: mysql_real_escape_string(): A link to the server could not be established
So with my existing development setup [both web and db server together], I am not able to see the PDO related errors.
Any way to resolve this problem.
mysql_real_escape_string() tries to open a connection to a server with mysql_connect() if no connection exist. The default values for mysql_connect() are "localhost" and "root" without a password.
If you have the root account without a password in your development environment (which is a pretty common setup) this will work without problem, since a connection can be esteblished.
On the live environment on the other hand, the root user hopefully has a password set, so this call will fail.
In this case mysql_real_escape_string() will return false instead of your escaped value.
The solution: Use the mysqli or PDO equivalent of the function. Or open an additional connection using mysql_connect() with valid credentials for the time being so mysql_real_escape_string() can use it.
I have a database set up on a godaddy server. It is configured to allow remote access, and there are a couple of websites I'm running which need to access this data. It works when accessed from another godaddy site, and I can connect from my development environment both at work and home. We recently set up hosting with mydomain.com.
Here is the code block that triggered it:
function connect(){
$servername = "XX.XX.XXX.XX";
$dbusername = "databaseusername";
$dbpassword = "mahpassword";
$dbname = "databasename";
try{
$newMysql = new PDO("mysql:host=".$servername.";dbname=".$dbname, $dbusername, $dbpassword);
}
catch(PDOException $e){
echo 'connection Failed: '. $e->getMessage();
die;
}
}
and now I'm getting this error message on the new site:
connection Failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XX.XX.XXX.XX' (111)
The only problems I can think of is that either for some reason there are a limited number of IP addresses the MySQL database will connect to by default (which seems squirrely), I'm getting blocked by a firewall on the MySQL server (again.. doesn't make sense to me), or there is some setting on the mydomain hosting server disallowing remote requests (?)
I'm new to this kind of thing, so I'm open to any suggestions. I could probably just set up another database on the new site, but I don't want the hassle of keeping them synchronized if I don't need to. What might be wrong? Are there any workarounds?
[edit]
connected to remote database via console (mysql -h XX.XX.XXX.XX ...), the privileges were found under the information_schema database, a quick select * from SCHEMA_PRIVILEGES and select * from USER_PRIVILEGES shows that 'databaseusername'#'%' has sufficient privileges. Not that it helped me any, but maybe it'll help someone down the road.
[/edit]
As it has been more than a year since I asked this question, I suppose I need to answer it just to close it.
It turns out that godaddy had blocked mydomain.com servers via firewall ("Remote access" was limited). so in order to accomplish what I wanted to do, I had to copy and store the database on both sites.
I am using php to send a query to an existing database to use existing login credentials for a webbased program to allow access to a members section page. My web server is public and has a public ip as well as a private ip. However, my sql server is sitting on the local lan and does not have a public ip. I am very new to php, but I feel I have a done a good job writing it. the code works amazingly when I hosted the site for internal use with a dummy database on my pc. I set up Apache on my pc to run the php code and set up a sql database for testing purposes. I could access the site and login and out from any other computer in the office, by typing in my 192.168.x.x into the web browser.
Now that the site is moved to the web server and I am linking to an active database on another server it doesnt want to work. I am pretty sure i dont have any coding erors causing this its a configuration issue. I am wondering what ports should be open where? and will existing DB users be able to query the DB from a remote private ip? I realize this may be a beginners question, but I have looked everywhere for days now and my brain is fried. I need a basic checklist of the main things to look for or set when establishing this type of connection.
Website is running on 192.168.1.1 with public ip of 173.72.173.x
SQL DB is running on 192.168.1.2
I log into the sql engine on the sql server with 'user' and 'password' so my config file i use:
$server = "192.168.1.2:3306"; // server to connect to.
$database = "myusers"; // the name of the database.
$db_user = "user"; // mysql username to access the database with.
$db_pass = "password"; // mysql password to access the database with.
$table = "dbo.users"; // the table that this script will set up and use.
And I call it into every file that need to query anything.
So are theses setting right? Or do I need to create another user on my sql engine to access the db from a remote host? and what about ports? sql server has 3306 opened, but that's it?
If you are using MySQL (you mentioned port 3306), you will most likely want to check the my.cnf configuration file on the MySQL server, and have it allow remote connections.
Just do a fast search for bind-address = 127.0.0.1 and comment it by putting a hash# in front of it and disable interface binding.
Since you did not specify the OS you are using, I can't know for sure whether it may be bound to a specific NIC or not. Try it yourself.