Thursday, January 10, 2013

Spreadsheet Programs Vs Databases


The Primary Key
By Stephon Unomon
Google +

 

       Spreadsheet programs (such as Microsoft Excel) have row numbers to identify what row a certain bit of data is in.

Databases have a similar identifier, called the Primary Key. You can draw out your database with relative ease - or you can create a mock up of it in a spreadsheet.

 

 
How do I remember what the primary key is when I make a new row of data?

 

When you create the tables, there are going to be more options that you need to include to make your database a little easier to use...specifically being AUTO_INCREMENT and NULL / NOT NULL. First, the code example...

 

 

Real World Code Example:

 

<?php

// Make a MySQL Connection

mysql_connect("localhost", "user", "password") or die(mysql_error());

mysql_select_db("test_db") or die(mysql_error());

 

// Create a MySQL table in the selected database

mysql_query("CREATE TABLE Employees(

id INT NOT NULL AUTO_INCREMENT,

PRIMARY KEY(id),

 employee_id INT,

 first_name VARCHAR(50),

 last_name VARCHAR(50)")

 or die(mysql_error()); 

 

echo "Table Created!";

 

?>

 

Let’s run through this one, step by step.

 

First, we make a connection to the MySQL database and select the database we want to work on.

 

Then, we make a table called “Employees”, with a column called “id” that will automatically increment by 1 each time a new record is created, has a NOT NULL value (this simply means that the “id” value is real and searchable), and that the Primary Key is set to be the “id” field. Now, we create our three data fields (employee_id, first_name, last_name), and issue the good ol’ “or die” command to tell us if something went wrong.
 
Check out one of the best PHP Editors
 

 

Thursday, December 27, 2012

Common MySql Field Data Types


MySql : Common Field Data Types:
By Stephon Unomon
Google +

 

CHAR() – Fixed length field between 0 and 255 characters that can store any type of data. The length of the field is set regardless of the size of the data placed into it.

DATE – Date in YYYY-MM-DD format.

INT – Whole numbers only.  The number of digits can also be specified in parentheses, but is not necessary.

TEXT – Variable length text-only field with a maximum length of 65535 characters.

TIME – Time in hh:mm:ss format.

VARCHAR() – Variable length field between 0 and 255 characters that can store any type of data.  Unlike the CHAR() type, the length of each VARCHAR() field is determined by the data placed into it.

 

So, knowing this, let’s decipher our code example from above.

 

<?php mysql_query(“

CREATE TABLE Employees

(open the query to the DB and make this table with Employees as the name)

 

(employee_id INT,

(make this field with employee_id as the name and allow only whole numbers)

 

first_name VARCHAR(50),

(make this field with first_name as the name, allow any input, and limit it to 50 characters in length)

 

last_name VARCHAR(50)”)

(make this field with last_name as the name, allow any input, and limit it to 50 characters in length)

?>

Once you get the basic concepts, data manipulation with MySQL is not difficult. Let’s dive in a little bit more to the way tables are laid out.
 
 
Save Time While Building PHP Applications
Check Out
The #1 PHP GUI Controls

MySQL Creating Tables - Lessons For Beginners


Create Tables with MYSQL_QUERY
By Stephon Unomon
Google +

 

Now that we’ve selected the database we want to use, we need to put some tables in the database in order to store data. Before that happens, though, you need to think about what kind of data is going in to your tables. I’ll get to why in a moment. Here’s a simple example of table creation with the mysql_query command:

 

<?php mysql_query(“

CREATE TABLE Employees

(employee_id INT,

first_name VARCHAR(50),

last_name VARCHAR(50)”)

?>

You’ll need to define the type of data that is going in to each table field...which is why you’ll need to put some thought into your database before you start creating tables.

 

On the next page lies some common types of data. Remember, if you try to put text (TEXT) into a field that is defined to take numbers (INT), you’re going to get errors! (It’s the square-peg-round-hole scenario!)
 
Do You Want To Get The Most Out Of PHP?
Check Out
The #1 PHP TOOLS

Tuesday, December 25, 2012

MySQL Data Base Select Command - PHP For Beginners (MySQL)


Putting The Data In The Base
By Stephon Unomon
Google +

 

OK! We’re connected to a MySQL database! Far out! Now what? We need to put data IN to the database and use it!

 

So, here’s how we do it.

 

 

 

First of all, you have to select the database with the mysql_select_db() command.

 

<?php

mysql_select_db("my_db") or die(mysql_error());

?>
 

Tuesday, December 18, 2012

MySQL How To : PHP For Beginners Lesson 13

MySql How To
By Stephon Unomon
Google +
Making the connection to MySQL...
 

 
Here we will go over MySql as a part of the Beginner PHP Course.  This important to know so I put together a Mysql How To

First off, we need to be able to tell PHP to connect to a database before we can use it. The command is straight forward - mysql_connect().

 

<?php

$dbhost = "localhost"; // the db’s server

$dbname = "mysite_dbname"; // the db’s name

$dbuser = "mysite_dbuser"; // the db’s username

$dbpass = "password"; // the password for the user

mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error()); //connect to db or show error if failure

echo "Connected to database";

?>

Looking at the above code, everything is pretty self explanatory...except the “or die” thing. This is a method of handling errors. If for some reason PHP cannot connect to the database, the “or die(mysql_error())” part tells PHP to show us exactly what the error was. Most of the time it is human error - the wrong username, password, etc. was entered in the code. Check your work!
 
Hope You Enjoyed This Mysql How To
 

Friday, December 14, 2012

PHP Database Function - PHP For Beginners Lesson 12


More Functionality with a database
By Stephon Unomon
Google +

 

       Nowadays you hardly ever hear PHP mentioned without MySQL close behind. Why? Well, PHP and MySQL make a great pair for web application development efforts. MySQL is often used in conjunction with PHP for the same reasons PHP is so popular - it is free, widely available, and most web hosts have it installed. There are other database systems that PHP works with, but MySQL seems to be the most common.

 

       In this section, I’ll familiarize you with using MySQL to connect to a MySQL database and perform the basics. Advanced MySQL and it’s SQL language is a little beyond the scope of this book, so we won’t dive very far in to heavy details (I don’t want to lose you!). However, MySQL’s commands are written in almost-plain-english, and you can usually figure it out just by looking at it.

 

       Most other PHP/MySQL books also tell you how to install MySQL and configure it. We’re not going to do that here. Your web host most likely has MySQL available for you, and they have simple ways to create databases. The two hosts that I have been using for years are pair Networks and Hostgator. Hostgator uses CPanel, and pair Networks uses a custom built control panel - both make it very easy to set up a database, and they provide the information you need to connect to it with.

 

So, let’s get connected!

With This:

 

Section 2 Overview - PHP For Beginners - Section 2 Wrap Up


Section Two Overview
By Stephon Unomon
Google +

 

In this section, we covered some more advanced features of PHP. Not for the timid, but not impossible to grasp!

 

IF - Do something if something is true.

ELSE - Do something else if it isn’t

ELSEIF - Add more choices to IF

SWITCH - Use this if you have a lot of ElseIf’s

ARRAY - One variable, many values

ASSOCIATIVE ARRAY - One variable, even more values

LOOPS - For, While, For Each - A way to repeat code

FUNCTIONS - Turn a big code chunk into a small one

SESSIONS - Carry unique data from page to page

COOKIES - Store session data on your viewer’s computer

FORM PROCESSING - Process web forms, such as a mailer

 

 

QUIZ TIME!

 

1. What are the 3 ways to comment code in PHP?

 

 

 

2. ELSEIF cannot be used without ____________

 

 

 

3. What are Cookies used for in PHP?
 
 
Check Out This Great