Hello, in this post I will cover: connecting to a MySQL server, selecting a database and requesting some simple information.
You will need:
MySQL
Apache with PHP
and a way to manage your MySQL such as PHPMyAdmin (PMA) - you can do it through the console.
If you installed XAMPP you will have all of these ready (To access PMA goto http://localhost/phpmyadmin in your web browser).
Variables
First we need to set are variables:
<?php $SQLhost = "localhost"; $SQLuser = "root"; $SQLpass = "password"; $SQLdb = "test";The values should be:
Host: The IP of your MySQL server, if you installed it on the same computer as the web server (Like you would with XAMPP) just set it to localhost.
User: The user to connect to the MySQL database with, you can leave this but I recommend you do not use root in a production environment.
Pass: The specified user's password (you will be asked to set this up in the MySQL installation- if you are on a webhost look for a MySQL option in their user management area).
DB: The database to use in the previous tutorial we setup 'test'.
Connecting and selecting
Ok now we need to connect to MySQL and select are DB:
mysql_connect($SQLhost, $SQLuser, $SQLpass) or die(mysql_error()); mysql_select_db($SQLdb) or die(mysql_error()); ?>Two simple functions: connect to our host and then select the db we want to work in. I recommend you save this PHP document and try to run it. If either of these fail the page will stop running and output a error. Ok well it appears we are done for this part:
Save this document as connect.php, we will use it next time!


Sign In
Create Account


Back to top









