Beginners PHP 3: Introduction to MySQL

5 posts Page 1 of 1
Contributors
User avatar
Alex
Member
Member
Posts: 40
Joined: Mon Feb 01, 2010 8:17 pm

Beginners PHP 3: Introduction to MySQL
Alex
In the previous tutorial, you have learned about the ways you can use the POST function in PHP. In this tutorial, you will learn about using MySQL in your websites.

What will we learn today about MySQL?
  • How to connect to MySQL and a Database
  • The die() function.
  • How to execute SQL to your database.
What you will need:
  • A MySQL database(No duh...)
How to get a MySQL database:
You can either download it from: http://mysql.com/.
Or you can sign up at a webhost that provides free PHP and MySQL. I recommend 000Webhost, found here: http://000Webhost.com/

Lets get started!
MySQL is a database that you can use for websites. It stores information, for example: PHP forums run on a database. It can be MySQL, PostgreSQL, MsSQL, or any other database out there. I recommend MySQL because it's lightweight, easy to use, and secure.
The function to connect to a MySQL database is the mysql_connect($host, $user, $pass) function, and it has 3 parameters. They are:
  • $host: The host of the database. If you downloaded it, it will be "localhost", if you got 000Webhost, it will be something like "mysql06.000webhost.com" or any other server, like mysql07.
  • $user: The username for your database. If you downloaded it, it will be "root". If you got 000Webhost, it will be whatever you set it to be.
  • $pass: The password for your database. If you downloaded it, it will be what you set it to be. If you got 000Webhost, it will also be what you set it to be.
None of these parameters can be blank.(None are optional)

To connect to a database, REQUIRED if you connect to a database, the syntax is mysql_select_db(string $db [, resource $link_identifier] ).
The parameters are:
  • $db = The name of the database, or schema, you are connecting to. Must be lowercased.
  • $link_identifier= The name of the connection you're using. (OPTIONAL)
For example if you typed:
Code: Select all
<?php
$connecttodb = mysql_connect("localhost","root","password");
?>
Then the syntax for connecting to a database would be:
Code: Select all
mysql_select_db("testdb",$connecttodb);
 
If you don't set mysql_connect("localhost","root","password"); to a variable, then you don't need to use the $link_identifier paramater.

The die() function:
If you want some text to appear, or anything like that, when your website fails at connecting to your MySQL or database, you will use the die() function.
It's used like:
Code: Select all
mysql_connect("localhost","root","password") or die("Could not connect");
 
Same can be used for selecting the database.
Code: Select all
mysql_select_db("testdb") or die("Could not select database");
 
Executing SQL Queries:
The syntax used for executing sql queries is mysql_query(string $query [, resource $link_identifier ] )
The parameters for this are:
  • $query: The query you are identifying.
  • $link_identifier: Same as mysql_select_db. (OPTIONAL)
An example of this would be: (Creating a table)
Code: Select all
mysql_query("CREATE TABLE users (
id int(11) AUTO_INCREMENT NOT NULL,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY(id)
);");
This creates a table, Users with 3 fields.
  • id: integer(11) as you insert something into it, it auto increments, and it cannot be null.
  • username: varchar(255) and it cannot be null.
  • password: varchar(255) and it cannot be null.
Then we set the primary to "id".
Now lets insert something into it.
Code: Select all
mysql_query("INSERT INTO users (username, password) VALUES ('alex','password')");
 
This inserts into the users table with the values of "alex" and "password" into "username" and "password".

Wrap it up
Well, we have now learned how to connect, select a database, die() function, and how to execute SQL queries into your database. Remember, this is only an introduction. You will learn more as you follow through in my tutorials.

Notes:
  • Connect to MySQL using the mysql_connect($host, $user, $pass) function.
  • Connect to a MySQL database using the mysql_select_db(string $db [, resource $link_identifier] ) function.
  • The die() function uses "or" inbetween functions.
  • Execute DB queries using the mysql_query(string $query [, resource $link_identifier] ) function.
Please comment with constructive criticism.. You should definitely know the rest by now.

Thanks,
Alex.
<?php

$wants_to_make_guide_today = true;
if($wants_to_make_guide_today == true){
make_guide();
}else{
sleep();
}
?>
User avatar
vbstuff
New Member
New Member
Posts: 19
Joined: Wed Feb 02, 2011 7:49 pm

very good for begginers
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

I love it, but can you post tutorials for databases in Visual Basic .NET too?
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Your format of tuts is really nice. Makes it more attractive!
Image
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Please become more active and post tutorials about php
You are a very good article writer ! such good tutorials are very rare these times
http://vagex.com/?ref=25000
5 posts Page 1 of 1
Return to “Tutorials”