Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Getting Started With Mysql, creating tables and insert data into them.
flashy
post Apr 2 2008, 03:56 PM
Post #1


Member [Level 1]
****

Group: [HOSTED]
Posts: 62
Joined: 7-March 08
From: Swindon, England
Member No.: 59,016



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!
Go to the top of the page
 
+Quote Post
shadowx
post Apr 2 2008, 07:10 PM
Post #2


A clever man learns from his own mistakes, a WISE man learns from those of OTHERS
*********

Group: [HOSTED]
Posts: 884
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719



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!
Go to the top of the page
 
+Quote Post
Absolute Zer0
post Apr 2 2008, 08:49 PM
Post #3


Member [Level 2]
*****

Group: [HOSTED]
Posts: 82
Joined: 21-March 08
Member No.: 59,631



Wow, very well written, I already feel like learning MySQL more!... After I learn PHP... laugh.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Complete Login System(56)
  2. A Guide To Css And Creating A Stylesheet(15)
  3. Complete Login And Registration System(9)
  4. Creating Your Own Icon(23)
  5. Php Dynamic Signatures(9)
  6. Delete Files And Directories Using Php(7)
  7. Searching With Php And Mysql(2)
  8. Creating A Simple Image Viewer(3)
  9. Creating A Timer Program(8)
  10. Installing Php + Mysql + Apache + Phpmyadmin On Windows Part 2(0)
  11. Quiz With Php, But Without Mysql(3)
  12. How To Create Tables(1)
  13. Tutorial: Creating Custom Icons For Devices(0)
  14. Check Referrer To Prevent Linking Yours From Other Sites(8)
  15. Starting Or Stopping Apache And Mysql Server Via Batch File(0)
  1. Cpanel Mysql Database Management(6)
  2. Simple Shoutbox(34)
  3. Simple User System(19)
  4. Adding Data To A Database And Displaying It Later(1)
  5. Php Form Data And Conditional Statements(2)
  6. Programming In Glut (lesson 1)(0)
  7. Programming In Glut (lesson 4)(0)
  8. How To Group Multiple Sets Of Data In Microsoft Excel 2007(0)
  9. Creating A Resume(1)
  10. Creating Navigation For Html Websites(12)
  11. How To Get Started Fishing: What You Will Need (uk)(2)
  12. How To Get Started Fishing: At The Lake(0)
  13. How To: Html Tables.(8)


 



- Lo-Fi Version Time is now: 20th July 2008 - 08:38 PM