Gammalt
Här har jag samlat lite innehåll från den gamla siten:
Tutorial 1 - Basic syntax (select, insert, update, delete)
So let's go though some basic SQL query's.
First, this is what you need:
A configured MYSQL, MSSQL or any other will probably do.
To your help it's recomended to use either "phpMyAdmin" or "SQL Server Management Studio"
Need to know how to create tables and databases (there's plenty of tutorials out there that'll learn you how, google it)
So, lets start.
I'll use a table looking something like this
Members
-----------------------------------------------------------------------
|Id(int)|Firstname(varchar)|Lastname(varchar)|Country|
-----------------------------------------------------------------------
First we want to insert some data into the table, this is done by an INSERT command,
eg. INSERT INTO [table] (field1, field2 ...) VALUES ('value1', 'value2' ...)
First you tell the database what command you will use (INSERT), then you want to tell it where to put it (INTO TableName), what we do then is telling it what columns should be used ((Column1, Column2).
Now when we know that we want to insert the data into "Column1" and "Column2" we add the last command which is the data that will be put(VALUES ('Value1', 'Value2'))).
Be sure to add quotes "'" at the start and end of each value added, Integers can be added without, but if you have a string you'll get errors if you forget to add them.
So, let's att some data to our table
--You can add a comment to your sql like this
--If "Id" is set as and auto increament(mysql) or identity(mssql) be sure to exclude the "Id" column from the query
INSERT INTO Members (Id, Firstname, Lastname, Country) VALUES (1, 'Tobias', 'Hartung', 'Sweden'); INSERT INTO Members (Id, Firstname, Lastname, Country) VALUES (2, 'Jonas', 'Martin', 'Denmark'); INSERT INTO Members (Id, Firstname, Lastname, Country) VALUES (3, 'Olof', 'Hartung', 'Sweden'); INSERT INTO Members (Id, Firstname, Lastname, Country) VALUES (4, 'Olof', 'Nilsson', 'Norway'); INSERT INTO Members (Id, Firstname, Lastname, Country) VALUES (5, 'Martin', 'Svensson', 'Denmark')So, now we've inserted our data into the database, you can add more data if you want, but lets go on. Let's go through the SELECT command. Here's a list of some ways of using the SELECT command: --Get all data from the table, "*" means all Fields(Columns)
SELECT * FROM Members --Get all members from Sweden SELECT * FROM Members WHERE Country = 'Sweden' --Get all members with lastname "Hartung" SELECT * FROM Members WHERE Lastname = 'Hartung' --GET Id From members with the name "Tobias Hartung" SELECT Id FROM Members WHERE Firstname = 'Tobias' AND Lastname = 'Hartung'The SELECT command is used whenever you need to retrieve data from a database table. Next up is the UPDATE command, this is used to update data in a table. We say user with "Id: 2" has changed his lastname to 'Hurtig', this is how you do. Remeber it's important to use an "Id" field in the table to be sure you just changing the row you want.
UPDATE Members SET Lastname = 'Hurtig' WHERE Id = 2 --This will change all users with the lastname "Hartung"'s firstname to "Tobias" UPDATE Members SET Firstname = 'Tobias' WHERE Lastname = 'Hartung' --This will change every record in the table's Firstname to "Tobias" --This would be usefull if you add a new field to the table and want to set a default value for all members UPDATE Members SET Firstname = 'Tobias'And now, the last command that's the DELETE command. Whenever you want to delete a record this is the command ;)
--Delete member with "Id:5" DELETE FROM Members WHERE Id = 5 --Delete members with Lastname "Nilsson" DELETE FROM Members WHERE Lastname = 'Nilsson' --Delete all records in the table DELETE FROM MembersSo, here is the basics, "w3schools.com" have some great tutorials about diffrent sql commands that's a great resource. You find it here. The next steps will be more advanced, this was just the basics, and all of the basics that you'll learn at this site.
PHP MSSQL - Get identity from inserted row
If you have an MSSQL database (dunno if it works the same way for mysql) and wants to get the "ID" from the row just inserted, read on... Oh, and your server need to have support for mssql and php, read on: http://www.userscape.com/helpdesk/index.php?pg=kb.page&id=13 To find out more, a newer version of that file can be found somewhere in the installation folder of "sql server 2000" if your're having probs. Ok so first we add the dabase stuff:
function Connect() {
// Connect to the database
$link = mssql_connect('SERVERNAME', 'username', 'password') or die('Something wrong, password maybe!');
// Select the database
$selected = mssql_select_db('DatabaseName', $link') or die('Probs man, db selection went wrong, check my code 2');
}
function Close() {
mssql_close(); // Close database
}
Put this in another file or something, you choose, but don't forget to include the file if you choose to put code somewhere else.
So, let's start what this page should explain.
Connect(); // Connect to db
// Insert query
$id = mssql_query("INSERT INTO table (row1, row2) VALUES ('value1', 'value2')
SELECT @@IDENTITY AS 'Identity'"); // Getting Identity
// Get value of Identity
$Identity = mssql_result($id, 0, Identity);
Close(); // Close db
// Showing the Identity in the browser, or do something else with it
echo 'The Identity value of the row just inserted is: ' . $Identity;
And woalla...
PHP Basics Part 1
I'm jumping the part with installing a server and all that stuff, but if you want to get startet google "XAMPP" that'll work for starters. It includes Apache with PHP and mySql and some other stuff like filezilla and some mail server. So now lets get to writing some code. For starters, alla PHP code needs to be inside . If you would like to write out "My name is Tobias", you write it like this"echo" and "print" does the same thing, it's up to you what you like the best. I usually just goes with "echo". Almost all code sould end with a semicolon ";" That's to tell the server that this part of the code has ended. When working with web designer it's most likely they'll go accedently deleting some of the code, so a tip instead of using the echo statement just end the php "?>" and write the html. This way it'll also make it easier to see the visual preview in for example Adobe Dreamweaver. To illustrate what i meant just go:
My name is Tobias
PHP Basics Part 2
Welcome to part 2, in this article we'll go through the things explained below. So, know I gonna explain variables, variables is something that stores data, for example you got stuff in a box, so know i gonna show you how to store your stuff in the box and then get it out. First all variables in PHP start with the dollar "$" mark. The code:The box $myVariable contains a string, by simply putting the content inside ' and ' . Next we have the $formStuff box, this one contains content from a form item, posted from another page. Inside ['here'] you put the name of the form item you want to get the data from.I'll probably show this later how it's done but don't be to sure. And last we got a QueryString box (or atleast if think it's called that), this gets the content from an url. If you for example add "?qString=boxContent" after the url to the page, the box content of the variable $queryString will be "boxContent". When you want to show the box(variable) content somewhere on your page, just put:
and "varName" is the actual name of the variable you most likely have created. PHP can sense what type of data a variable contains, if it's Integers, text or something else. In C#, Java for example you have to tell the script what type of content a variable should have. Yea, I think that's it for now, start experimenting with the stuff. Good Luck until next time! Next part we'll go through Variables I guess, might be some loops involved to.
