Showing posts with label mysql for beginners. Show all posts
Showing posts with label mysql for beginners. Show all posts

Thursday, December 27, 2012

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 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