Showing posts with label php database. Show all posts
Showing posts with label php database. Show all posts

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
 

 

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: