Hi all, I'm a beginner to coding webpages. Not sure where I should be posting this, but I saw this article "Fundamental PHP Know-How" in this forum, so I figured to ask here.
For the past few months, I've been doing my php development by editing code, then ftp-ing the php files onto my host, then running the webpage to test if it works.
Then I realize I should have installed apache + mysql + php on my computer.
I have learnt to code basic php, however the problem I have now is I'm not exactly sure about the big picture here, because the whole time I have just kind of taken things as it is, doing connections to database which is already setup on the host, so long as it 'magically' works.
Can anyone help me out with a layman explanation of how these 3 work together?
My understanding now
1. mysql is the database where i store my data,
2. php is the language, and therefore when I install it on my computer, it will be able to read php code?
3. I have really no idea what apache is for.. Well wiki says its a web server, but what exactly is that - to compile my php/html codes and make it do whatever it should?
I also read that I have to connect to my database to "localhost", in that case, when I want to upload my stuff to my web host, I would have to change all my connection codes. Is there an elegant way to do this?
Thanks a bunch!
Apache + MySql + PHP Question
Moderator: General Moderators
Re: Apache + MySql + PHP Question
Hi,
always good to see someone wanting to learn more about the big web.
So to your questions:
1. Yes MySQL is a database to store your data. A Database can have multiple tables, that would be like a lot of files in the filesystem, where you have a lot of data listed, like in excel. What is great about a database, it stores the data very efficiently and can retrieve the neccassary data extremly fast.
2. PHP is an interpreter, which takes your php code an parses and executes it.
3. Apache is an webserver which your browser communicates with, based on the HTTP-Protocoll. Apache is modular and can execute the PHP Interpreter whenever needed. Example would be when a file with the .php extension is called.
Hope that brings a bit light into the darkness
EDIT: did miss your last question somehow
Yes there is an elegant way, create an config.php file, which you include in your php files whenever you need your connection configs.
always good to see someone wanting to learn more about the big web.
So to your questions:
1. Yes MySQL is a database to store your data. A Database can have multiple tables, that would be like a lot of files in the filesystem, where you have a lot of data listed, like in excel. What is great about a database, it stores the data very efficiently and can retrieve the neccassary data extremly fast.
2. PHP is an interpreter, which takes your php code an parses and executes it.
3. Apache is an webserver which your browser communicates with, based on the HTTP-Protocoll. Apache is modular and can execute the PHP Interpreter whenever needed. Example would be when a file with the .php extension is called.
Hope that brings a bit light into the darkness
EDIT: did miss your last question somehow
Re: Apache + MySql + PHP Question
I have moved this thread to the General Discussion forum. PHP Code forum is for questions or discussions of PHP code.
genix2011 has given you a start on understanding, but let me add a few points that may help you further:
A web server is the software that SENDS web pages to a browser. Every time you use a web browser (IE, Firefox, Safari, Chrome, Opera) to visit a web page, your browser sends a REQUEST to the web server that you address or link to (http://www.google.com, whitehouse.gov, mit.edu, facebook.com.....). If that address can be found, and if a particular document you are requesting can be found there, the web server is the software that sends that document back so your browser can display it. Probably 99% of the web servers in the world are either Microsoft's Internet Information Server (IIS) or Apache, mostly dependent on whether the site's servers are running under Windows or Linux. Without a web server, you can't view a web page that has any PHP code in it, because PHP is a server-side language, interpreted in the web server before it sends the page to the browser. A browser can display a plain HTML file without a web server, but not if there is any PHP code involved. By contrast, Javascript is a client-side language, interpreted in the browser. As you know, HTML is the markup language that instructs a browser how to display a web page.
Thus, when developing web pages with PHP, it's normal practice to have a web server installed on your "development" computer (at home, in your case) so you can test new ideas without exposing all your experimentation to the whole world by developing on your "production" server. It's also easier and faster, since you don't have to constantly use FTP to upload new versions to the production server. Fortunately, Apache is fairly easy to install on either a Windows or a Linux computer, so that's what most developers do. PHP parsing and execution is built into Apache (and can be added to IIS), so all you need to do is download the PHP libraries and configure Apache so it knows where to look for them.
MySQL is probably the most popular serious database in the world. It is used by the largest companies and organizations for mission-critical applications that require top performance and reliability under heavy use by large numbers of users. MySQL is also relatively easy to install on even a modest home computer and is supported by PHP so you can connect to it, send queries, and receive recordsets from it.
So those are the 3 elements of setting up a local "development system" on your own computer so you can write PHP code in web script files, test them on your local machine, then when everything is working properly, FTP them to the "production" server.
The standard "address" of a web server (Apache or IIS) that is installed on the same machine as the browser that's requesting a web page from it is localhost. So when you have installed, say Apache, on your desktop or laptop, and you open a browser on the same computer, instead of entering something like "http://youtube.com", you enter "http://localhost/index.php" or just "localhost" or "localhost/xyz.html".
Be aware that there ARE a few differences, because the environment (available software, directory structure, etc.) is likely to be at least a little different between your "development" and "production" systems. So don't be shocked if, occasionally, something works one place and not the other. As an example, you probably won't have an SMTP (email) server installed at home, so you may not be able to test a script that sends email.
Yes, there are "clean" solutions to most of the differences, such as your MySQL connections. Here are a few examples:
PHP maintains a lot of "server environment variables", such as the name and path of the currently running script. These special global variables all begin with $_ followed by all-uppercase names. See http://php.net/manual/en/reserved.variables.php and especially, click on $_SERVER.
By using PHP's include() or require() functions to establish your MySQL connection, and in the include file, testing to see if the global variable $_SERVER['SERVER_NAME'] is localhost, as shown below, your script can supply the appropriate host name, user name, and password for the MySQL database, depending on whether the server is or is not localhost.(by the way, I have changed the information above, it's not real)
With an include file like the above (with proper values, of course), each PHP script that needs to connect to the MySQL database would have a line like this:and that would work correctly whether you ran it locally or on the production server.
That doesn't eliminate all the issues, because your production database may contain different data than your development database, so there are many other situations that you will need to consider, but that should give you some idea of the process.
genix2011 has given you a start on understanding, but let me add a few points that may help you further:
A web server is the software that SENDS web pages to a browser. Every time you use a web browser (IE, Firefox, Safari, Chrome, Opera) to visit a web page, your browser sends a REQUEST to the web server that you address or link to (http://www.google.com, whitehouse.gov, mit.edu, facebook.com.....). If that address can be found, and if a particular document you are requesting can be found there, the web server is the software that sends that document back so your browser can display it. Probably 99% of the web servers in the world are either Microsoft's Internet Information Server (IIS) or Apache, mostly dependent on whether the site's servers are running under Windows or Linux. Without a web server, you can't view a web page that has any PHP code in it, because PHP is a server-side language, interpreted in the web server before it sends the page to the browser. A browser can display a plain HTML file without a web server, but not if there is any PHP code involved. By contrast, Javascript is a client-side language, interpreted in the browser. As you know, HTML is the markup language that instructs a browser how to display a web page.
Thus, when developing web pages with PHP, it's normal practice to have a web server installed on your "development" computer (at home, in your case) so you can test new ideas without exposing all your experimentation to the whole world by developing on your "production" server. It's also easier and faster, since you don't have to constantly use FTP to upload new versions to the production server. Fortunately, Apache is fairly easy to install on either a Windows or a Linux computer, so that's what most developers do. PHP parsing and execution is built into Apache (and can be added to IIS), so all you need to do is download the PHP libraries and configure Apache so it knows where to look for them.
MySQL is probably the most popular serious database in the world. It is used by the largest companies and organizations for mission-critical applications that require top performance and reliability under heavy use by large numbers of users. MySQL is also relatively easy to install on even a modest home computer and is supported by PHP so you can connect to it, send queries, and receive recordsets from it.
So those are the 3 elements of setting up a local "development system" on your own computer so you can write PHP code in web script files, test them on your local machine, then when everything is working properly, FTP them to the "production" server.
The standard "address" of a web server (Apache or IIS) that is installed on the same machine as the browser that's requesting a web page from it is localhost. So when you have installed, say Apache, on your desktop or laptop, and you open a browser on the same computer, instead of entering something like "http://youtube.com", you enter "http://localhost/index.php" or just "localhost" or "localhost/xyz.html".
Be aware that there ARE a few differences, because the environment (available software, directory structure, etc.) is likely to be at least a little different between your "development" and "production" systems. So don't be shocked if, occasionally, something works one place and not the other. As an example, you probably won't have an SMTP (email) server installed at home, so you may not be able to test a script that sends email.
Yes, there are "clean" solutions to most of the differences, such as your MySQL connections. Here are a few examples:
PHP maintains a lot of "server environment variables", such as the name and path of the currently running script. These special global variables all begin with $_ followed by all-uppercase names. See http://php.net/manual/en/reserved.variables.php and especially, click on $_SERVER.
By using PHP's include() or require() functions to establish your MySQL connection, and in the include file, testing to see if the global variable $_SERVER['SERVER_NAME'] is localhost, as shown below, your script can supply the appropriate host name, user name, and password for the MySQL database, depending on whether the server is or is not localhost.
Code: Select all
<?php
if($_SERVER['SERVER_NAME'] == 'localhost' ) {
$env = "devel";
} else {
$env = "prod";
}
if($env == "devel") {
$host = "localhost";
$user = "root";
$pwd = "";
} else {
$host = "fairbanks.db.1077304.hostedresource.com";
$user = "wurlitzer";
$pwd = "XYZ123";
}
mysql_connect($host, $user, $pwd);
mysql_select_db("aardvark");
?>With an include file like the above (with proper values, of course), each PHP script that needs to connect to the MySQL database would have a line like this:
Code: Select all
...
include("dbconnect.php");
...That doesn't eliminate all the issues, because your production database may contain different data than your development database, so there are many other situations that you will need to consider, but that should give you some idea of the process.
Re: Apache + MySql + PHP Question
Hey genix and calif,
Guys, I never thought I could get such joy from reading a forum reply, but reading your replies ABSOLUTELY made my day
Been looking all over the place for answers, spending hours trying to piece everything together.
Understanding those made my setup of AMP actually make sense now, instead of just blindly following online installation guides, editing paths in config files which I have no slightest clue as to what exactly each of those step does..
After that I had to know how to work with files on localhost and transfer to web host elegantly, or setting up localhost would be all but meaningless - and those were just PERFECT examples calif.
I really think this should go onto stickies on one of the forums
For self-taught folks who are starting out, this will help a great deal - I know I'd probably be more than where I am right now if I had "started right" 
Thank you so much guys.
Guys, I never thought I could get such joy from reading a forum reply, but reading your replies ABSOLUTELY made my day
Understanding those made my setup of AMP actually make sense now, instead of just blindly following online installation guides, editing paths in config files which I have no slightest clue as to what exactly each of those step does..
After that I had to know how to work with files on localhost and transfer to web host elegantly, or setting up localhost would be all but meaningless - and those were just PERFECT examples calif.
I really think this should go onto stickies on one of the forums
Thank you so much guys.
Re: Apache + MySql + PHP Question
You're certainly welcome. Thanks for the kind words. We like to think that PHP DevNetwork forums are among the better forums in helping all levels of PHP coders, whether they are beginners or advanced professionals. As you get going with PHP development, come back with new questions (you're likely to have some). 
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Apache + MySql + PHP Question
@califdon that's quite an exhaustive introduction. Bookmarked 
Re: Apache + MySql + PHP Question
My recommendation, find a usable computer you can use, google for the "perfect server" tutorials from howtoforge.com and build yoursel an ispconfig3 linux server. After some work you'll end up with a real-world php/mysql/apache/dns/ftp server with a perfectly usable web control panel to manage it. Throw it on your LAN and voila, you have adevelopment server!
Of just build the server in a virtual machine.
I have a couple I use for php application development and testing, it's well worth the few hours it takes to follow the tutorial and build the server.
Of just build the server in a virtual machine.
I have a couple I use for php application development and testing, it's well worth the few hours it takes to follow the tutorial and build the server.