Jump to content
xisto Community
Sign in to follow this  
flashy

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

Recommended Posts

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.

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, Xisto 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.

// connect to the databasemysql_connect("localhost", "admin", "pass") or die(mysql_error());mysql_select_db("test") or die(mysql_error());//create a tablemysql_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
// connect to the databasemysql_connect("localhost", "admin", "pass") or die(mysql_error());mysql_select_db("test") or die(mysql_error());// insert into tablesmysql_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
// connect to the databasemysql_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!

Share this post


Link to post
Share on other sites

Nice little tutorial :lol: 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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.