I'm currently studying PHP/ MySQL and i'm just curious how does one actually connect the PHP page to the HTML and to the MySQL?
So, it's helpful to explain at this point that there is a divide of sorts between these technologies. PHP and MySQL are both executed server-side. HTML is client-side. What usually happens is that the PHP application connects to the MySQL server, does its thing by sending any number of queries to the server and processing the results, then generates the HTML that is transmitted downwire to the client. Make sense?
Sort of... but how do I actually connect them? Do I reference like I do when i'm referencing a javascript doc or a CSS doc?
No, you need to create a PHP file on a server that has the capability to process PHP files. The way it works is that whenever you access a PHP file on a server that can process them, it runs the PHP file through the PHP interpreter (essentially running the PHP as a program) then sends the resulting HTML to the document. So, all you should need to do is create a PHP file, stick it inside some server software that can handle it, then navigate your browser to the URL for the php file. Most people use Apache for the web server. If you don't have any preference and are running on a Windows machine, check out http://www.wampserver.com/en/ -- it should give you the entire stack with little fuss. You should also look at the documentation on php.net
Your PHP file sits in a directory on the server, just like an HTML file would. Actually it's just an HTML page with additional PHP directives that just get converted to HTML before the server sends it out. That PHP code contains the information needed to connect to a MYSQL server (you need to provide this information), for example: <?php $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code ?>
Thanks guys, both of those are pretty helpful :)
I would add that the easiest way to think about this is that PHP is referred to as a "templating language" at times because essentially you create a script in php that templates an html page for display and uses your databases information as well as the users interactions and the rules that you have created to fill in the details of that final template. Now matter how complex your program gets in php you are still filling in the blanks of an html template that you create for the user interface.
Join our real-time social learning platform and learn together with your friends!