|
|
|
|
![]() ![]() |
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! |
|
|
|
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
I always found creating tables using PHP versus PMA really difficult to remember the syntax but in truth there isnt much to it! |
|
|
|
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...
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 20th July 2008 - 08:38 PM |