Nov 21, 2009

Getting Started With Mysql - creating tables and insert data into them.

free web hosting
Open Discussion > MODERATED AREA > Tutorials

Getting Started With Mysql - creating tables and insert data into them.

flashy
Hi in this tutorial you will learn how to create tables and insert items into them.

First steps are to create the database - go into your cpanel and mysql databases, from there make an account and a database and then attach them together with all priviliges, call the database test and the account admin, with the pw as pass - or any other password.
We need to connect to the database so first in your php file (probably named index.php) - this is how to do it.

CODE
mysql_connect("localhost", "admin", "pass") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

The first line was to connect to the host as LOCALHOST as it is usual the databases locally, the username would be what the username would be (most servers require you to place your CPANEL username as a prefix - like PREFIX_USER, trap17 requires you to do so), and then your password.

The second line was to connect to the database that you created earlier, again you willneed to place in the prefix if you did so for the username.

Now we can start to make a table! We will make a guestbook in this tutorial.

CODE
// connect to the database
mysql_connect("localhost", "admin", "pass") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

//create a table
mysql_query("CREATE TABLE guestbook(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30),
comment VARCHAR(500))") or die(mysql_error());

The two lines starting from 'id' to '(id)' are the row id's - you'll see what i mean when you go into phpMyAdmin.
VARCHAR means variable characters - so any variable (abc!"£123) can be placed into it, you can use INT for numbers.
the VARCHAR '(30)' means how many characters are stored into this variable.

Now lets go insert some data into the tables
CODE
// connect to the database
mysql_connect("localhost", "admin", "pass") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// insert into tables
mysql_query("INSERT INTO guestbook(name, comment) VALUES('Jim', 'Hi nice site!')") or die(mysql_error());
mysql_query("INSERT INTO guestbook(name, comment) VALUES('Bob', 'Lovely weather')") or die(mysql_error());

That created two lines of data into 'guestbook', 2 names - Jim and bob, and 2 comments - 'Hi nice site' and 'Lovely weather'

Now lets retrieve the variables in the database - but we must store them as an array and read them as an array
CODE
// connect to the database
mysql_connect("localhost", "admin", "pass") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$get = mysql_query("SELECT * FROM guestbook") or die(mysql_error());

while($array = mysql_fetch_array($get)){
echo "Name: ".$array['name']."<br />";
echo "Comment:<br />".$array['comment']."<br /><br />";
}

The * after SELECT means ALL - in other words SELECT ALL FROM guestbook.

I hoped i helped getting started!

 

 

 


Comment/Reply (w/o sign-up)

shadowx
Nice little tutorial smile.gif It would be good if you continued the series to modify table contents and structure with DELETE, UPDATE, SORT etc...

I always found creating tables using PHP versus PMA really difficult to remember the syntax but in truth there isnt much to it!

Comment/Reply (w/o sign-up)

Absolute Zer0
Wow, very well written, I already feel like learning MySQL more!... After I learn PHP... laugh.gif

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Started Mysql Creating Tables Insert Data

  1. Install Php 5 To Work With Mysql - (1)
    hi, all php 5 is new to all people a long time( actually, I forget how long...). it introduced a
    more object-oriented design to developer. like, access type(public, private, protected), abstract
    type(classes that can't create objects), interface support, and more . althrought it is
    widerly available, someone still can't get it running with mysql. every time the php is starting
    up. a message prompted stated that some xtensions can not be found, and therefore, not loaded. so, I
    have written this to help other and newbie get it start quickly. note: thi...
  2. Delete Files And Directories Using Php - following up from creating and writing (11)
    How To Delete Files and Directories follow up from creating them Hello all and
    welcome to my second tutorial involving file management. In my previous tutorial , I explained how
    to create, write and read files. In this tutorial I'll explain how to remove the files and
    directories you took so long to create. I did not explain last time how to create directories as I
    did not know, now I do, you can use the mkdir() function. Now with this tutorial.... Removing
    Files Removing files can easily be done with the unlink() function: CODE unlink("...
  3. Check Referrer To Prevent Linking Yours From Other Sites - Check referrer with Php and Mysql (9)
    Check Referrer Using Php To Prevent People Linking To Your Downloads From Other Sites Ever
    find that found some people are listing items, images and tuts and linking directly to the download
    url (those that are like my photoshop tutorial.php?id=0), which is a .php to count the number of
    downloads. To prevent this, you can add a piece of code to the download pages that checks which page
    referred them to the download page: if it's my domain, it downloads the file normally, if
    it's not, it will redirect to my home page instead. Important : Not all browser...
  4. Php Form Data And Conditional Statements - My third tutorial (3)
    Intro To PHP Tutorial 3 - Form Data and Conditional Statements Released 4/12/07 By Chris Feilbach
    aka GhostRider Contact Info: E-mail: assembler7@gmail.com AIM: emptybinder78 Yahoo:
    drunkonmarshmellows Website: http://www.ghostrider.trap17.com PART I - FORM DATA One of the
    things that makes PHP so popular is its ability to handle form data. You can do whatever you want
    with it, you can email it to yourself, store it in a database, display it on a page, you can do
    literally ANYTHING with it. There are two ways to send data to your server, GET and POST. Both
    are ...
  5. Php/mysql Login/register - Tutorial for login with databases. (4)
    Start register code. Register.php CODE Username: Email: Pass: Verify
    Pass: //Login to your database. Make the fields of course..
    mysql_connect("localhost","user","pass"); mysql_select_db("database"); //end //if registering,
    check fields. if ($action == register) { if (!$user || !$pass || !$email || !$vpass) {  print
    "You must fill out all fields.";  exit; } $dupe1 = mysql_num_rows(mysql_query("select * from
    table where user='$user'")); if ($dupe1 > 0) {  print "Someone already has that
    username.";  exit...
  6. Clicks Counter - With PHP + MySQL (1)
    Well, in this tut, I'll show you how to do an simple link counter, with only one count for each
    IP and the date when the link were clicked. Just remember, this is only one way to do it. First,
    create an mysql database called 'links', with 2 table: first table will be called
    'links', with the columns: id, title, ref and clicks. The other one will be 3 columns and
    will be called links_info: id, ip and date. Just remember that the columns 'id' of this
    second table IS NOT auto-increment. Here is the code of the file which will count the clicks, ...
  7. Simple User System - php, mysql driven (21)
    Hey! Maybe you've seen my other tutorials...or my signature.. Anyways I'm going to show you
    how to make a system so users of your site could register accounts and you could have protected -
    user only - pages on your site /smile.gif" style="vertical-align:middle" emoid=":)" border="0"
    alt="smile.gif" /> Ok, so we start by creating a config.php file. CODE     $dbhost   =
    'database host';     $dbname   = 'database name';     $dbusername   = 'database
    username';     $dbuserpass = 'database password';          mysql_connec...
  8. Creating Common Navigation For A Website: Part 2 - Newbies guide to creating common navigation with PHP (0)
  9. How To Start Your First Game Project - The first steps to creating your own game! (1)
    Ok, I see alot of questions around the game development forums of people wanting to make their first
    game, but they don't know where to start. So, I decided to write this tutorial, having been in
    the same situation before. NOTE: This Tutorial: -Is meant to help you get past that initial bout of
    developer's block -Does not teach you how to make a game, just help you start -Cannot help you
    if you cannot answer the questions asked So, let's get to it, shall we? How to start
    your first game project: --So, the first thing you need to make sure yo...
  10. Create Dynamic Html/php Pages Using Simple Vb.net Code - Taking your application data, and creating a webpage for others to vie (1)
    This example will show you how use a string in VB to create PHP code. In order to do this, you need
    a string to store your PHP page and a function that I will list at the bottom of the page for you to
    put in a module. This code is written in VB.NET Public Sub CreatePage(ByVal HTMLTitle As
    String, ByVal HTMLText As String, ByVal HTMLFileName As String) Dim strFile As String '
    ---------------------- ' -- Prepare String -- ' ---------------------- strFile = "" '
    -------------------- ' -- Write Starter -- ' -------------------- strFile = " " ...
  11. Getting Started With Amfphp And Rias - first steps in creating RIAs (2)
    AMFPHP in a short way is a library of php files that let u manage in JUST ONE FILE what u would do
    in many files like for example queries to mysql. So u can have tons of queries to mysql and all of
    them in just ONE FILE! so what is a RIA? a RIA is a Rich Internet Application commonly given to
    flash applications, not the animations or presentations we see daily on the internet but very useful
    and cool programs made in flash. the fisrt step u need to take is to download the AMFPHP library
    directly from its site at www.amfphp.org, in some free hosted sites the latest ver...
  12. Complete Login System - With PHP + MYSQL (57)
    Its an complete login sistem made and tested by me and I think itwill be very usefull for people who
    are tryn to learn PHP. First, let's make register.php: CODE include("conn.php"); //
    create a file with all the database connections if($do_register){ // if the submit button were
    clicked if((!$name) || (!$email) || (!$age) || (!$login) || (!$password) || (!$password2)){ print
    "You can't let any fields in blank.\n"; // if the user did not put some field exit; } $name =
    stripslashes($name); $email = stripslashes($email); $age = stripslashes($age); $login = s...
  13. How To: Html Tables. - I find these really useful. (8)
    If you are a novice web designer, but want your site to look advanced and proffessional, then what
    better way to do so than to use HTML tables? HTML tables are a really easy way of formatting your
    text, to make your ste look proffesional. It looks good, and its easy, what more can you ask for?
    You have to use the tag, so first lets start off with: HTML Table > /table > In
    between theese two tags, we will add the data for the table, using the tags and . =Table
    Heading (Title) =Table Data (what you want in the table) =table row (new row) So, lets ...
  14. How To Get Started Fishing: What You Will Need (uk) - This applies mainly to the UK but other countries too. What you will n (2)
    Hi folks, with the beginning of spring its time to crack out the coarse fishing rods and land a few
    fish, i was supposed to be fishing today but the weather took a turn for the worse so instead let me
    try to convince a few of you to go fishing and take your kids too! I dont need to explain what
    fishing is, everyone knows that but let me clear up a few misconceptions: 1) Do you eat the fish?
    Or do you kill the fish? Or otherwise remove them from the lake? A) No! There is a nasty habit of
    some eastern Europeans to remove and eat the fish from lakes and that might be fine...
  15. How To Get Started Fishing: At The Lake - mainly applies to UK, see the "what you will need" too! (0)
    So you now know what you need to start fishing from my other tutorial, once you have it all it's
    time to head to the lake! As i said pick up the bait on the way, maggots from the tackle shop, corn
    and spam from tescos and bread too if you want (you can eat it too of course!). At the lake you can
    ask those already fishing for their tips but some anglers are liable to lie to keep the best spots
    to themselves. So when you get look for the "swims" These are areas along the bank where the bushes
    have been cleared and sometimes paving slabs have been laid to let you sit ...
  16. Creating A Timer Program - Using Visual Basic 2005 (8)
    This tutorial will explain how to create a basic timer using Visual Basic Express 2005. If you
    don't have it, it's free and you can dowload it from Microsoft's website. All you need
    is a few minutes to sit down and read this and a version of Visual Basic. OK, so what will this
    timer actually do? Well, you are able to enter a number of minutes and a message, and then click a
    button. Once the timer is up, your message pops up and you are reminded! So, basically it's a
    little reminder system. I use it to remind me when TV programmes start, when I have to go ...
  17. Creating Navigation For Html Websites - Have a common navigation menu for the whole website! (12)
    Pre-requisite: HTML, inline frame tags 1 Attachment(.zip) included. Updates : 29-12-07: Doctype
    added in example files (Advised by jlhaslip) Designing a whole website takes a lot of planning
    and organization. Designing a proper navigation system is a basic step in building your website. If
    you are developing webpages in html you would have observed that as you go on creating pages it
    becomes difficult to maintain the links to the pages. This article will guide you in developing a
    common navigation menu for your website. It describes three ways, so if you don'...
  18. Complete Login And Registration System - doesn't use mysql! (9)
    kLogin 0.1 QUOTE(readme.txt) Readme file to kLogin 0.1 To use the internet explorer fix:
    download the latest IE7 ZIP file
    (http://sourceforge.net/project/showfiles.php?group_id=109983&package_id=119707) Extract the ie7
    zip file to the root directory of your web server. Example, if you are using a unix/linux server,
    it's on "public_html/" or "home/public_html" Open kLogin.php file with your editor and edit the
    $info_text or $info_txt variable. Then, extract the kLogin.php file in to the root directory of
    your web server also. Just run kShoutBox.php If ...
  19. Adding Data To A Database And Displaying It Later - Using Forms, PHP and MySQL (1)
    Requirements: PHP Support MySQL Database access I am going to use a news program as an example.
    Ok, first you are going to need to connect to the database. Do so by using the code below. I have
    added some comments where you will need to edit to fit your server's specifications. Create a
    new file with notepad and call it config.php QUOTE //Change root to your database
    account's username $dbusername = "root"; //Add your account's password in between the
    quotations $password = " "; //Add the name of the database you are using in between the qu...
  20. Simple Shoutbox - PHP, MySQL driven.. (34)
    Ok, so I'm going to show you how to create a very basic shoutbox which is driven with PHP and a
    MySQL database. So, lets start - open a database management program like PHPMyAdmin and run these
    queries. SQL CREATE TABLE `shoutbox` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT , `name`
    VARCHAR( 255 ) NOT NULL , `mail` VARCHAR( 255 ) NOT NULL , `time` VARCHAR( 255 ) NOT NULL ,
    `message` TEXT NOT NULL , `ip` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `id` ) ) TYPE = MYISAM ;
    CREATE TABLE `shoutbox_admin` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT , `name` VARCHAR( 255...
  21. Creating A Resume - 10 Tips For Making A Resume (1)
    I've been working on my Resume for months now. Here is a summary of what I've learned: 1.
    Avoid referring to yourself via 1st person or 3rd person terms. Rather than saying "I started this
    job in" just say "Began job in"... Employers expect Resumes to be professional and avoid reference
    to oneself; and instead speaking in an impersonal tone that presents
    achievements/skills/experience/education without personalization. Avoid words like "I", "my", "he",
    "she", etc. Leave out personal pronouns and only use the action words/verbs. This also includes
    your Ob...
  22. Creating A Simple Image Viewer - Using Visual Basic 2005 Express Edition (4)
    I downloaded Microsoft's Visual Studio Express suite a few months ago, but only recently got
    around to installing it. I have been practising with Visual Basic and making some rather basic
    programs and utilities, but they contain most of the basic concepts. This tutorial will explain how
    to create a basic image viewer, and I will try to explain each step from beginning to end as clear
    as I can. To start you will need: Microsoft Visual Studio About 10-20 minutes free time OK,
    first open up the Visual Basic part of the Studio. I am using the 2005 Express version, so...
  23. How To Create Tables - (1)
    At the end of this tutorial you should know how to: 1.How to create a database on your sever
    2.How to create tables on a mysql database For a script 3.How to confirm your script so that
    it works with your database TUTORIAL PART 1 How
    do we create a database using the cpanel on a server? Well i'll explain it the best way i can.In
    your cpanel go to yor mysql options.Once in you will easeily see than option to create a database
    and an option to create a user. Frist go to the create database option and t...
  24. How To Group Multiple Sets Of Data In Microsoft Excel 2007 - (0)
    How to Group Multiple Sets of Data in Microsoft Excel 2007 Sometimes you may open several
    workbooks and work with a number of the same workbooks at a time. You can open this group of files
    with Microsoft Excel 2007 simultaneously. But you have to define them as part of a workspace, and
    save them in a single Excel 2007 file. To do this, follow below steps: 1- Click On the View tab and
    then in the Window group click Save Workspace. The Save Workspace dialog box appears. 2- In the File
    name field, type your work name and then Click Save. 3- At the top-left of window, C...
  25. Programming In Glut (lesson 4) - Creating 3D objects (0)
    Lesson 4 of 6. I hope you are enjoying them /laugh.gif" style="vertical-align:middle"
    emoid=":lol:" border="0" alt="laugh.gif" /> . QUOTE Hello, in this tutorial we will be creating
    a 3D pyramid. We are building this tutorial from Lesson 3, but I took out the 2D objects and placed
    a 3D pyramid in there instead. The 3rd axis for drawing can be a litle confusing, but after you get
    the hand of it you'll do fine. Now when you are setting a 3D vertex just remember that the
    camera is on the positive end of the z axis. So things that have a more positive z axis va...
  26. Programming In Glut (lesson 1) - Creating a windwo (0)
    This is the first of six lessons I am transferring from Astahost for programming in GLUT, and after
    the six I hope to make more, I hope you enjoy. QUOTE Hello, I'm starting a series on how to
    program in OpenGL using the OpenGL Utility Toolkit, a.k.a. GLUT. I chose GLUT because it is quick
    and easy to write, and very easy to learn. In this tutorial I am going to teach you how to create a
    basic window which we will build off of in later tutorials. Throughout the tutorial I will leave
    notes to let you know what each command does, and how you can modify it to fit...
  27. Creating Your Own Icon - (23)
    It is easy to create your own icon, just pick a bitmap (.bmp) file and change its extension to .ico.
    To do so, open the Windows Explorer, click on the View menu (or Tools in WinMe), click Folder
    Options, click View tab, remove the check on the "Hide file extensions for known files types"
    option, and then click OK. Select a bitmap file, press F2 key, and then change its extension to
    .ico. Have fun learning:)...
  28. Cpanel Mysql Database Management - Part 2.1 Of My 7 Part Cpanel Tutorial (6)
    This tutorial is an extension to my 7 tutorial series about the Cpanel. The 7 different Cpanel
    tutorials can be found below. Part 1: E-mail Management Part 2: Useful Site Management Tools
    Part 3: Useful Site Management Tools2.1 Part 4: Analysis/Log Files Part 5: Advanced Tools Part
    6: PreInstalled Scripts, Extras, and Cpanel Options Part 7: Fantastico Detailed Cpanel
    Tutorial Part 2.1: MySQL Management In this tutorial, i will explain how to add, edit, delete
    and over-all manage the MySQL Feature in the Cpanel. Creating a MySQL Database 1) ...
  29. Starting Or Stopping Apache And Mysql Server Via Batch File - (0)
    Hi guys, this is a litte tutorial about how we start and stop the Apache and MySQL in Windows NT
    (2000, XP, 2003) via a batch file script. As we know in Windows NT based system Apache and MySQL
    installed as Windows Services. So we can stop and start it using NET command. For more information
    about DOS command, type HELP at command prompt. I assuming that your MySQL service name is "mysql"
    and your Apache (Apache 2.0.x) service name is "apache2". If you want to chek it click Start > Run >
    services.msc > OK. Windows IS NOT Case Sensitive. Let's get started!. 1. Open...
  30. A Guide To Css And Creating A Stylesheet - (15)
    Table of Contents: I. Introduction II. Starting your stylesheet --A. Starting syntax with
    font-family --B. Defining classes --C. Using classes III. The STYLE tag IV. Comments in CSS V. The
    "a" tag VI. A quick list of common attributes VII. Notes --A. Universal classes --B. Grouping --C.
    Multiple instances VIII. Finding other attributes IX. Closing I. Introduction Firstly, to begin
    using a stylesheet, you must have one. Open up your text editor and save as (something).css. I know
    NotePad doesn't need quotes for a stylesheet, but I'm not sure about other progr...



Looking for started, mysql, creating, tables, insert, data,
Install Php
5 To Work
With Mysql
Delete Files
And
Directories
Using Php
following up
from
creating and
writing
Check
Referrer To
Prevent
Linking
Yours From
Other Sites
Check
referrer
with Php and
Mysql
Php Form
Data And
Conditional
Statements
My third
tutorial
Php/mysql
Login/regist
er Tutorial
for login
with
databases.
Clicks
Counter With
PHP + MySQL
Simple User
System php,
mysql driven
Creating
Common
Navigation
For A
Website:
Part 2
Newbies
guide to
creating
common
navigation
with PHP
How To Start
Your First
Game Project
The first
steps to
creating
your own
game!
Create
Dynamic
Html/php
Pages Using
Simple
Vb.net Code
Taking your
application
data, and
creating a
webpage for
others to
vie
Getting
Started With
Amfphp And
Rias first
steps in
creating
RIAs
Complete
Login System
With PHP +
MYSQL
How To: Html
Tables. I
find these
really
useful.
How To Get
Started
Fishing:
What You
Will Need
(uk) This
applies
mainly to
the UK but
other
countries
too. What
you will n
How To Get
Started
Fishing: At
The Lake
mainly
applies to
UK, see the
"what
you will
need"
too!
Creating A
Timer
Program
Using Visual
Basic 2005
Creating
Navigation
For Html
Websites
Have a
common
navigation
menu for the
whole
website!
Complete
Login And
Registration
System
doesn't
use
mysql!
Adding Data
To A
Database And
Displaying
It Later
Using Forms,
PHP and
MySQL
Simple
Shoutbox
PHP, MySQL
driven..
Creating A
Resume 10
Tips For
Making A
Resume
Creating A
Simple Image
Viewer Using
Visual Basic
2005 Express
Edition
How To
Create
Tables
How To Group
Multiple
Sets Of Data
In Microsoft
Excel 2007
Programming
In Glut
(lesson 4)
Creating 3D
objects
Programming
In Glut
(lesson 1)
Creating a
windwo
Creating
Your Own
Icon
Cpanel Mysql
Database
Management
Part 2.1 Of
My 7 Part
Cpanel
Tutorial
Starting Or
Stopping
Apache And
Mysql Server
Via Batch
File
A Guide To
Css And
Creating A
Stylesheet

Searching Video's for started, mysql, creating, tables, insert, data,
See Also,
advertisement


Getting Started With Mysql - creating tables and insert data into them.

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com