Question

I've been developing PHP pages for several weeks now and am getting much more confident in my code. I've obtained a hosting account through Bluehost, and am ready to begin making some live pages. Previously I have been using XAMPP on Windows and developing all of my pages on my local computer. I'm trying to determine the best practice for creating my page locally and then easily moving it to my hosting server. I use Filezilla to transfer my files. Here are my main questions:

1.) How can I ensure my local and live MySQL databases stay in sync? I have been manually creating the database locally, and then taking the same code and applying it to my live server. (I have phpmyadmin locally and on the live server, but dont know how to use it to streamline this process)

2.) I have to change the mysql password and connection credentials on all files before moving them from local server to live server. Is there a way around this?

Was it helpful?

Solution

For question 1 you could dump the database and import it at the production end each time you do an upload, or you could use mysql replication.

For question 2 it is quite common to have the database connection details in a separate php file and then include that file at the top of each page that needs to connect to the database

Then you can just change that one file, or only upload the one with the production database details.

db.php

<?php
$dbuser="";
$dbpass="";
$dbname="";
$dbserver="";
?>

myfile.php

<?php
include("db.php");
$conn = mysql_connect($dbserver, $dbuser, $dbpass);
mysql_select_db($dbname);
//....
?>

OTHER TIPS

You could point your mysql_connect() host to your new server's IP address. Therefore, no matter where your code is, it will connect to the socket.

$link = mysql_connect('<your ip here>', 'mysql_user', 'mysql_password');

I always make my projects so that I have to change 1 line of code to fluctuate between dev and production.

I have a base file where i specify my environment like this:

$env = 'dev'

Then in my database setup variables i have something like this:

if($env == 'dev') {
    specify dev variables
} else if($env == 'prod') {
    specify prod variables
}

The beauty of this is that when i want to move between dev and prod, i only have to change 1 variable.

Use a version control system such as svn or git; look at phpmig for database management, look at deployment tools such as capistrano, make sure you test on a replica of the production server (even if you just use a virtual host)

I'm pretty much in the same position as you and here is how I plan to do it.

  1. I don't plan to sync my local database with the live one but just move the local database once to the online one when I'm done. The way I do that is just, in PHPMyAdmin select the database I want and export it as a .sql file. Then on the online server I simply import that file.

  2. The best way I know is to have a separate file where you have a function which looks something like this:

    function connectToDB(){ $mysqli = new mysqli("localhost", "root", "123", "myDatabase"); return $mysqli; }

    Then when you want to use that you just import the file in the top of your other PHP files and use it like this:

    $mysqli = connectToDB();

This is my way of doing it. If someone knows a better way, I would be happy know it.

Deploying from development to production is not a trivial task and can be very complicated. Companies invest a lot of resources in order to make deployment a logical and stable process. I recommend to break your question into pieces and ask more specific.

As response to your questions, what I'd do:

1) When deployment requires a change in the database structure, write those changes in sql scripts. When you deploy you'll need to run those scripts and update the code (as you can figure out, the time in between will make the application unstable or broken). Before anything, take a copy of the production database onto development and try the scripts first on development. You will get so databases in sync each time you deploy.

2) How you save passwords depends on whether you are using frameworks or not. Following frameworks, it's a good approach to define an environment variable and set passwords according to the environment (dev or production).

As regards to code deployment, even if you are a unique developer, I'd use svn or git to save your projects under versions control. It will make code updates much more simple and help you to roll back if desired provided you create tags for each deployment.

Best practices are to write database migration scripts, version them. Then each time you upload to production, you store the values of these versions in the database. Only run the versions that you have not run before.

Have different local config files outside of your repository. This allows you to maintain the necessary application config in the repo with a main config file, then the local config file overrides database and environment settings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top