Monday, March 4, 2013

Web-servers and database set ups

This is not going to be an in-depth post for advanced folks. Rather, this is a post for beginners to help them make heads and tails from all the things that seem to be so complicated. Or this can be treated as a base or auxiliary material after which you should go hit up Google for more information. You should just get a basic understanding of how things work out of this post.

Alright so. Database is a place where you keep your data - the stuff that you will need repeatedly or that will need to be updated or added in the same format. You can create many tables in a single database. And each table can have as many columns as you want. Each column needs to have a type - string, integer etc, can have a limitation of how many characters long will the type be like varchar 255.

For example if you're making a category menu for your website I'd rather put it into a database - this way I can easily change and add categories without having to go into the code, finding the right spot, and modifying the same code in many files.

Anyway. So if database is a place for data it also needs to be somewhere - needs to have a physical location just like anything. So, when you go host a website that provides you with a databse of any kind you should get an email from them letting you know the HOST name of that database, and other information. In this case setting it all up is easy - you either get an email or you can log in to the website that's doing the hosting and find all the info you need.

Now say you don't have a website, or didn't get a database with it but suddenly you need to test some stuff out with databases.

No problem there - go download WAMP servers (or something similar. WAMP is my personal preference) and install it. What happens is it creates a virtual server - in reality the server is on your computer there is a folder where all of the data, files, etc is stored. Now RUN the servers. This kind of server is accessible only on your computer - locally. Since it's local it got the name localhost.

Once this is done open your browser and type in localhost in the address bar and hit enter (or get there some other way). Go to phpmyadmin. Create a database there - can be empty (no tables).

On the right of your phpmyadmin homepage there is all kinds of information:

  • Server: localhost (localhost via TCP/IP)
  • Software: MySQL
  • Software version: 5.5.24-log - MySQL Community Server (GPL)
  • Protocol version: 10
  • User: root@localhost
  • Server charset: UTF-8 Unicode (utf8)

  • This means your HOST NAME or SERVER is localhost.
    Your database is MySQL (there are many other databases like Django etc. MySQL is a database but not all databases are MySQL :D )
    and the default user is root . root@localhost is the same as myName@gmail.com gmail is the SERVER and myName is the user. so if you're logging into gmail you only input the name before @. but if you're giving your email to somebody it will be the whole thing. Remember that localhost is running locally :)

    By user they mean database user. Like admin or just somebody who is allowed to view or edit the data in the database. This is not a user of your website that needs to register. Different things.

    By default the password for root user is empty.

    So this is enough information for your page to be able to connect to your database. Here's an example in PHP:

    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

    $dbname = 'myDB';
    mysql_select_db($dbname);


    Of course you can just do :

    mysql_connect('localhost', 'root', '');
    mysql_select_db('myDB');

    MySQL_connect is a function that takes STRING arguments so everything is in quotes. Since there is no password you write an empty string - '' or "".

    If you are using a framework or software where you're give fields to fill out you write all of that without quotes. Here's a wordpress example:

    Database Name: myDB (your database. can be empty but must be in existence :) )
    Username: root
    Password : (leave blank)
    Database Host: localhost
    Table Prefix: wp_ (your tables in that database will always start with wp_ )

    This is a very basic set-up and is quite simple you need at very minimum 4 things - server / host , username, password, database name. That's it. Usually there are tooltips or descriptions that tell you what the field needs to be - make sure to read it. Don't panic if something doesn't work - maybe you need to provide additional information like port number or what have you. Read the documentation, look things up on your localhost, just Google for a specific question. Trust me you're not the only person with that problem - There are communities online where you can ask questions and get answers and solutions and if you Google for something you will most likely come across a solution exactly for your problem :)

     

    No comments:

    Post a Comment