Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Chit-Chat (http://www.chiefdelphi.com/forums/forumdisplay.php?f=14)
-   -   Teach Genia How to Program and Win a Prize (http://www.chiefdelphi.com/forums/showthread.php?t=35161)

DarkJedi613 21-02-2005 20:57

Re: Teach Genia How to Program and Win a Prize
 
Quote:

Originally Posted by Xufer
yea i don't pride myself on neatness infact my code isn't formatted or even commented at all i just did the commenting for Eugenia...

Besides that you didn't follow anything though. Usually things are named for what they mean, not for just random words. Plus second words are usually capped. :)

Amanda Morrison 21-02-2005 21:06

Re: Teach Genia How to Program and Win a Prize
 
Yeah, so I'm not a comp sci major anymore, but indeed I was once, many moons ago. I thought I liked technology... I later realized I just loved language in general. However...

I bought this book when I was a senior in high school. It never left my desk at home during that year, and early into college. For just beginning, especially with if/then statements and such, this is an excellent reference. That, or go to a local college's bookstore (in Genia's case, Purdue) and look for the CS courses. Look for low numbered classes, like 101, 102, etc., and look at the books that are required for the class. These are often really good books to look at (and, instead of buying it, you may be able to get some for really cheap on Amazon or from the local library).

MisterX 22-02-2005 16:09

Re: Teach Genia How to Program and Win a Prize
 
Quote:

Originally Posted by DarkJedi613
Besides that you didn't follow anything though. Usually things are named for what they mean, not for just random words. Plus second words are usually capped. :)

HAHA I guess it just runs in the team. I do the mechanics stuff on the team but being a senior I figured it would be fun to take a programming course so I ended up taking a Java course. Long story short even our teacher can't understand what I am doing but she says as long as it gets you where you wanna go it doesnt matter how many sidesteps I take.

Some of the words I've created Wrecktangle, Wreckedtangle, Advntrr, Xplrr, Byse, UhHuh, UhUh .... and that is just for my greeter.

Mike 22-02-2005 20:51

Re: Teach Genia How to Program and Win a Prize
 
Code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace First_Project
{
        public class WinForm : System.Windows.Forms.Form
        {
                private System.ComponentModel.Container components = null;
                public System.Windows.Forms.Label lblChalkboard;
                public System.Windows.Forms.Button btnLearn;
                public System.Windows.Forms.Button btnDontLearn;

                public WinForm()
                {
                        InitializeComponent();
                }

                protected override void Dispose (bool disposing)
                {
                        if (disposing)
                        {
                                if (components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose(disposing);
                }

                #region Windows Form Designer generated code

                private void InitializeComponent()
                {
                        this.lblChalkboard = new System.Windows.Forms.Label();
                        this.btnLearn = new System.Windows.Forms.Button();
                        this.btnDontLearn = new System.Windows.Forms.Button();
                        this.SuspendLayout();

                        this.lblChalkboard.Location = new System.Drawing.Point(32, 88);
                        this.lblChalkboard.Name = "lblChalkboard";
                        this.lblChalkboard.Size = new System.Drawing.Size(240, 48);
                        this.lblChalkboard.TabIndex = 0;
                        this.lblChalkboard.Text = "Do you want to learn?";

                        this.btnLearn.Location = new System.Drawing.Point(32, 176);
                        this.btnLearn.Name = "btnLearn";
                        this.btnLearn.Size = new System.Drawing.Size(72, 24);
                        this.btnLearn.TabIndex = 1;
                        this.btnLearn.Text = "Learn";
                        this.btnLearn.Click += new System.EventHandler(this.btnLearn_Click);

                        this.btnDontLearn.Location = new System.Drawing.Point(128, 176);
                        this.btnDontLearn.Name = "btnDontLearn";
                        this.btnDontLearn.Size = new System.Drawing.Size(80, 24);
                        this.btnDontLearn.TabIndex = 2;
                        this.btnDontLearn.Text = "Dont Learn";

                        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                        this.ClientSize = new System.Drawing.Size(292, 273);
                        this.Controls.Add(this.btnDontLearn);
                        this.Controls.Add(this.btnLearn);
                        this.Controls.Add(this.lblChalkboard);
                        this.Name = "WinForm";
                        this.Text = "Teach Genia OOP C#";
                        this.ResumeLayout(false);
                }
                #endregion


                [STAThread]
                static void Main()
                {
                        Application.Run(new WinForm());
                }
               
                private void btnLearn_Click(object sender, System.EventArgs e)
                {
                        lblChalkboard.Text = "I'm not a good teacher, go search on google :P";
                }
       
                private void btnDontLearn_Click(object sender, System.EventArgs e)
                {
                        lblChalkboard.Text = "Yeah... that was probably a safe bet, huh.";
                }

        }
}

(I started learning C# today...)

EDIT: It's .Text not .text, like I said, I started today.
EDIT 2: This wasn't meant to be taken serious, I highly don't recommend learning C# as your beginning language, however I just found my old PHP tutorials so I'll post those.

jgannon 22-02-2005 20:54

Re: Teach Genia How to Program and Win a Prize
 
Quote:

Originally Posted by Eugenia Gabrielov
Here it is everyone! My first code! thank you to all who contributed.
Most especially darkjedi :)

Code:

passesLegendOfMaxOnHeavy(person) {
    if (person==Genia) {
          return true;
    } else {
          return false;
    }
}
if(passesLegendOfMaxOnHeavy(Genia)) {
    awesome=1;
} else {
    triesAgain=1;
}

There you have it.

I think you need a return type on your function, and a type for the argument. Also, since the C18 compiler doesn't support boolean data types, you'll have to define them another way:
Code:

#define true 1
#define false 0
#define Genia 461

unsigned char passesLegendOfMaxOnHeavy(int person) {
    if (person==Genia) {
          return true;
    } else {
          return false;
    }
}

if(passesLegendOfMaxOnHeavy(Genia)) {
    awesome=1;
} else {
    triesAgain=1;
}

The #define statement for "Genia" allows you to create "friendly" names for people. For instance, you could also add:
Code:

#define dlavery 116
#define JVN 229

You wouldn't need those definitions if "person" was some sort of object defined in its own class, rather than an int, but it's been so long since I wrote classes in C, I wouldn't even know where to start. Trying to keep C/C++ and Java straight in your head is no walk in the park.

*removes nerd hat* I'm such a dork...

Nonetheless, awesome, Genia. You might just be a CS major in training, if you're willing to trade in your social life. :p ;)

Eugenia Gabrielov 23-02-2005 14:18

Re: Teach Genia How to Program and Win a Prize
 
What is this, social life you speak of?

I'm pretty sure you define it in a different way, you crazy programmer >_<

Mike 23-02-2005 17:43

Re: Teach Genia How to Program and Win a Prize
 
Here's some old PHP tutorials I wrote a while back, finally found them.

PHP Tutorials

PHP Tutorial Lesson 1

Introduction:
Hopefully by the end of these tutorials you will have enough knowledge to make your own web-based games. However there are alot of things you'll need to cover by yourself. I have also made this tutorial so that you won't need any fancy "Make a website in 24 hour" software. Dreamweaver is not needed. I recommend Crimson Editor. I will be making reference to it throughout the tutorials. Lesson 1 will be about Proper Coding.

Proper Coding
Of all the most annoying things I could single out easily one MAJOR problem among beginning and expert coders alike. The code layout, the code may work fine but it is hell on the eyes and if/when you come back to it in a while it will be very hard to understand. You should always write your code so that it is easily readable in case you get it wrong the first try, also if you were ever to hire a new coder this will make their job ALOT easier. Here are the basic principle elements

* User Friendlyness
* Legibility
* Finding Errors



User Friendlyness:
The way your scripts operate should be easy to understand without any confusing functions. A user may try to make a username such as Mike's Account, your program should be able to catch the ' and tell the user that it is an illegal character (because of the security risk it poses, you will learn about this later on). Now instead of automatically replacing the ' with say, a space, your script should notify the user that a ' is an illegal character. Following these guidlines will also help you keep down the need for support. Error messages should also be user friendly, you can get some pretty gruesome error messages that will confuse your users, with some simple error handling you can display a user friendly error and possibly give some links to notify your technical staff.

Legibility:
Your code should be readable, heres an example of some bad code
Code:

<?php
$the_password_that_the_guy_used = $_POST['password'];
if($the_password_that_the_guy_used == "password"){
$afdafdsaf = "welcome";
}else{
$afdafdsaf = "your not him!";
}
echo $afdafdsaf;
?>

Now there are a few things wrong with this code, first off there is no indentation. By indenting your code you are making it much easier to match up your brackets, your if/thens, and saving yourself alot of time! Yet another problem our Joe Newbie has is his looooooong variable names. Variables should get down to the point, using prefixes or whatever you need to use. Instead of $the_password_that_the_guy_used you could just use $password, this saves more time without having to type all that unneeded junk. His final problem is his odd choice for variable names. He is just asking for errors, this name is very easy to mistype, which would lead to an error, or displaying nothing at all (depending how your server is set up). After much review here is his new code
Code:

<?php
    $password = $_POST['password'];
    if($password == "password"){
          $message = "welcome";
    }else{
          $message = "your not him!";
    }
    echo $message;
?>

Finding Errors:
99.9% of your errors will come in this form


What I usually do in this situation is copy the problem part, google it, and read some answers to other peoples problems. If it is relevant to my problem then usually it fixes it, however if that doesn't work then I will open up my code in Crimson Editor and find the error line. One thing to remember about PHP is that it doesn't say what line the error is on but what line the page crashed on. For example if my bike got a flat tire at yard 10 and I hit a tree at yard 30 PHP would say that the error was on yard 30. Get it? Good.

Conclusion:
To wrap up the first part of my tutorial, i'd like to give some good php sites.
Help Sites
PHPFreaks Site
PHPFreaks Forums
PHP Enabled Free Hosting
Hostultra
Spunge *Recommended!*

Next Tutorial:
My next tutorial will cover variables and if/then statements. We will write some basic code to allow very simple user login.

Note:
If you have anything to contribute feel free to PM or email me

Mike 23-02-2005 20:06

Re: Teach Genia How to Program and Win a Prize
 
PHP Tutorial Lesson 2


Introduction:
During this tutorial I will teach you:

1. Using Variables
2. Comparing
3. If / Thens
4. POST / GET


By the end you should be able to make a simple user login form.

Some Stuff You Need To Know (Basic)

* <?php -Starts the php
* ?> -Ends the php
* Anything after a // is a comment and will not be shown on runtime
* All lines should end in a semi-colon (;) Except for If/Thens and Comments
* Echo outputs text to the browser
* Brackets ({ and }) let the code know that the if/then has started/finished


Using Variables:
Variables are objects that hold a chunk of information for you to use later on. Variables are always started with a dollar sign ($) and can have anything after that ($name, $password, etc.) One important thing to remember is that variables ARE case sensitive! A variable such as $name is different then a variable called $Name. An important thing to remember is that variables must start with either a letter or underscore (_). A variable name such as $1stGuy is invalid, however $_1stGuy is able to be used. Displaying variables with other strings is something unique to php, lets see this code for example.

Code:

<?php
    $name = "Mike";
    echo $name; //puts out Mike
    echo "My name is $name"; //puts out My name is Mike
    echo 'My name is $name'; //puts out My name is $name
?>

Comparing:
One important thing to remember about PHP is it's way of comparing and setting variables.
If you look in the code above you'll see that I used
Code:

$name = "Mike";
The single equal sign means that you want $name to have the value Mike. Now if I wanted to see if $name equalled Mike I would use the following code
Code:

<?php
    $name = "Mike";
    if($name == "Mike"){
          echo "Your name is Mike!";
    }
?>

I used two ='s because I was comparing the two instead of making them equal. This is somewhat hard to explain so I hope you follow me.

If / Then
As you have seen before i've been using many if(... statements. These are fairly easy to comprehend and are in this format



This is very usefull for creating a hard coded login and is essential if you ever plan on coding anything.

POST / GET
NOTE: Some HTML knowledge is required for this section.
POST and GET are two different ways of transfering information from one page to another. Here is our HTML form for POST on a page that I will call login.html

Code:

<HTML>
<BODY>
<FORM ACTION = "login.php" METHOD = "POST">
<INPUT TYPE = "text" NAME = "name">
<BR>
<INPUT TYPE = "password" NAME = "password">
<BR>
<INPUT TYPE = "submit" VALUE = "Login!">
</FORM>
</BODY>
</HTML>

Now you should see some key things "ACTION = "login.php"" for example is showing that it will submit this information to login.php which I will show you later on. "METHOD = "POST"" is saying to use the POST information transfer. Please remember that you need to have it in all uppercase, typing just "post" will give you an error. Lets get on with our login.php code
Code:

<?php
    $name = $_POST['name'];
    $password = $_POST['password'];
    if($name == "Mike" && $password = "Password"){
        echo "Welcome!";
    }else{
        echo "Invalid Login!";
    }
?>

OK one thing you may notice is the $_POST bit. What that is saying is the information that was POSTed will be assigned to a variable. Notice that the part inside the brackets is the same as the input field names in our html form? In my If/Then statement I used && to signify AND. Using GET is pretty much the same but with one small twist. Here again is our HTML code
Code:

<HTML>
<BODY>
<FORM ACTION = "login.php" METHOD = "GET">
<INPUT TYPE = "text" NAME = "name">
<BR>
<INPUT TYPE = "password" NAME = "password">
<BR>
<INPUT TYPE = "submit" VALUE = "Login!">
</FORM>
</BODY>
</HTML>

Notice the METHOD = "GET" part? Here is our login.php code

Code:

<?php
    $name = $_GET['name'];
    $password = $_GET['password'];
    if($name == "Mike" && $password = "Password"){
        echo "Welcome!";
    }else{
        echo "Invalid Login!";
    }
?>

Yet again the only thing changed is POST to GET. The difference between POST and GET is the URL. If I were to login correctly using GET my address bar would show
http://www.server.com/login.php?nam...ssword=Password
However if I was using POST it would just show
http://www.server.com/login.php
GET is good if you want something that can be bookmarked, etc.

Conclusion:
Hopefully you understand what this code does and how it does it. Copying and pasting will get you no where. You must understand what it is doing, try experimenting. See what you can do with this simple snippet, the possibilities are endless.

Next Tutorial:
Possibly writing to a file, and opening from a file. Maybe a multiple user login via txt files, possibly a quotes system?

Mike 23-02-2005 20:07

Re: Teach Genia How to Program and Win a Prize
 
PHP Tutorials Lesson 3
Introduction:
In this tutorial you will learn

1. Arrays
2. File Functions
3. Randomizing
4. Putting It Together


By the end you should be able to display random quotes, combine this with the previous tutorials and you should be able to make a form which adds a quote to a text file which then displays a random quote from that file.

Arrays:
An array is basically a list of something, that is numbered. This would be an array called dogs.

Dogs:
0. Golden Retriever
1. Rottweiler
2. Labrador Retriever

Notice that it starts at zero? In pretty much all languages arrays start at zero.
In php this would be
Code:

$dog = array("Golden Retriever", "Rottweiler", "Labrador Retriever");
//Now to display it we would use $dog[index] like so
echo $dog[0]; //shows Golden Retriever
echo $dog[1]; //shows Rottweiler
echo $dog[2]; //shows Labrador Retriever

You could also put variables in an array.
There is also another kind of array, say you don't want to remember what number it is, instead remember a keyword. This will also come in useful when you start in mysql.
Code:

$dog = array(
    "Golden Retriever" => "Sammy",
    "Rottweiler" => "Spot",
    "Labrador Retriever" => "Snickers",
    );
echo "My Golden Retrievers name is $dog['Golden Retriever'], my Rottweilers name is $dog['Rottweiler'], and my Labrador Retrievers name is $dog['Labrador Retriever']";
//that will show My Golden Retrievers name is Sammy, my Rottweilers name is Spot, and my Labrador Retrievers name is Snickers.

Some things to notice is the =>'s, im not sure if it was intended or not but they look sorta like arrows. Notice that the array can be set up multi-lined as well, as long as you open it with parenthesis and that you end it with a semi-colon. One important thing is that each line is seperated by a comma, not a semi-colon. The final part of arrays is multidimension arrays, it is somewhat like an array within an array. Take a look at this code:
Code:

$dog = array(
    "Golden Retriever" => array("Sammy", "Fast"),
    "Rottweiler" => array("Spot", "Lazy"),
    "Labrador Retriever" => array("Snickers", "Slow"),
    );
echo "My Golden Retriever's name is $dog['Golden Retriever'][0] and is $dog['Golden Retriever'][1], my Rottweiler's name is $dog['Rottweiler'][0] and is $dog['Rottweiler'][1], my Labrador Retriever's name is $dog['Labrador Retriever'][0] and is $dog['Labrador Retriever'][1].;
//That will show My Golden Retriever's name is Sammy and is Fast, my Rottweiler's name is Spot and is Lazy, my Labrador Retriever's name is Snickers and is Slow.

Now this information about arrays wasn't totally necessary for this tutorial, but instead of leaving you copying/pasting I decided to write it out so you'd know.

File Functions:
For this part we will use the following functions

* File
* Fopen
* Fwrite
* Fclose


File:
What file does is open up a file and put the info into an array seperated by each line. Lets take a look at this code
Code:

$quotes = file("quotes.txt");
In quotes.txt you have


File is automatically doing this
Code:

$file = array("This is a quote", "This is another quote", "Quote 3");
The syntax is
file(page);
NOTE: You do not need to use fopen when using file

Fopen:
As you may guess, this function opens a file for appending, writing, etc. The format is
$variable = fopen(page, mode);
$variable is because when you use other functions to write to it, etc, you have to reference back to the fopen. $variable is your reference point. Page is just what it says, the page you want to open. Mode is what you want to do with it, here are the modes that you need to know right now


Fwrite:
This writes to the file, obviously :P:. The syntax is
fwrite(variable, "what_you_want_to_write");
Remember I said you needed a variable in Fopen? That will be used here to tell the server what file to write too. Depending on your mode in Fopen it will either append, read, read/write, etc.

Fclose:
Fclose does just that, close the file to prevent corruption. Syntax is
fclose(variable);
Yet again using that reference point that I told you about when using fopen.

Randomizing:
For this part we will use the following functions
* Count
* Rand

Count:
Count counts the amount of things in a variable, if you want it to count the number of items in an array you could use a code like this

Code:

$dogs = array("Golden Retriever", "Rottweiler", "Labrador Retriever");
$count = count($dogs);
echo $count;
//will display 3

Notice that it's not starting at 0 like arrays do. Syntax is
count(item);

Rand:
Produces a random number between a given maximum and minimum integer, or a completely random number. Sample code:
Code:

echo rand() . "\n"; //Could display something like 2042
echo echo rand(1, 10); //Could display something like 5

NOTE: When creating a completely random number it actually creates a number between 0 and RAND_MAX. On some servers RAND_MAX is 32768. If you need something higher you will need to specify a maximum number higher then that.


Putting It All Together
To create our quotes system you first have to take your quotes and put them in an array

Code:

$quotes = file("quotes.txt");
Now you must randomly select a quote from that array, remember how it was sorted by numbers?
Code:

$quote = $quotes[rand(0, count($quotes) - 1)];
Lets look at this part for a bit
Code:

rand(0, count($quotes) - 1;
You are creating a random number with a starting point of zero and an ending point of the number of quotes minus one. Minus one because count starts with one and arrays start with zero, so in the array there is no quote four (considering you have an array of three).
So now your code is saying $quote = $quotes[random_number], just have it echo it and your done! Our full code is
Code:

<?php
    $quotes = file("quotes.txt");
    $quote = $quotes[rand(0, count($quotes) - 1)];
    echo $quote;
?>

Conclusion:
Hopefully you understand everything i've said so far. Using the fwrite function you should be able to write to quotes.txt, however remember that after each one you have to have a line break. Happy coding :)

Mike 23-02-2005 20:09

Re: Teach Genia How to Program and Win a Prize
 
PHP Tutorials Lesson 4

Note: The links to images have long since stopped working.

Introduction:
In this tutorial you will learn

1. Introduction to MySQL & phpMyAdmin
2. Connecting To Your Database
3. SQL Queries
4. Putting Information Into A Database
5. Extracting Information From A Database


Introduction to MySQL & phpMyAdmin:

MySQL:
What is MySQL?
Simply put, MySQL is a database system. SQL stands for Structured Query Language, which is exactly what it is: a language that queries information out of a database. MySQL is the most popular open source database because of it's speed and reliability. Most free hosts do NOT give you a MySQL database with their hosting package. Below is a list of the few that do.

1. Spunge
2. FreeSQL*
3. HomepageHost
4. HostRave

* Is only a database host, does not host sites

phpMyAdmin:
What is phpMyAdmin?
phpMyAdmin is a frontend to the MySQL server. It allows you to create, drop, and alter databases, run any line of SQL code, delete, edit, and add fields, and manage field keys.

NOTE: I censored my pics since they are vital to a site I am making now... you can never be too safe

Here is the welcome screen to phpMyAdmin

Over on the left under "Home" it shows you
Database Name
Table 1
Table 2

Now you click on a table and you are immediately brought to the Structure page of it

At the bottom you can see a textbox, this is where you can run your SQL statements. Above you can see the fields their type and their other info. The other pages that you can navigate to are shown at the top. For this tutorial I will be giving the most info on Browse and Structure pages. These are the most commonly used pages.

Click on the browse tab and you are brought here

Here you are shown the current contents of the table and you are given options to Edit it or Delete it. Although you cannot see it in this picture there is also a SQL textbox on the bottom.

SQL:
Just an SQL textbox.

Select:
Fancy options to extract data.

Insert:
Fancy options to insert data.

Export:
Back up your database.

Operations:
Move table, alter table, rename table, etc.

Empty:
Delete all info from the table

Drop:
Delete table.


Connecting To Your Database:

Includes:
Before you begin with this you must learn about the include() function. What the include does is INCLUDE a file within your file, for example if I had a site where on every page at the top it said "abcdedfghijklmnopqrstuvwxyz012345679" now I don't want to keep typing that over and over again so what I would do is I would make a file called alphabet.php that echo'ed the text. Then at the top of every page I would put

Code:

include("alphabet.php");
and of course it would show "abcdefghijklmnopqrstuvwxyz0123456789".

Connecting:
To connect to your database you need 4 things. A username, password, host, and database name. With most sites you need to connect to your database on every page so you make an include file called connect.php, in that file you have the following code.
Code:

<?php
    mysql_connect("server", "username", "password") or die("Could not connect to server");
    mysql_select_db("database_name") or die("Could not open database");
?>

What this does is connect to the server first, if it can't then it stops loading and displays an error message. Then it tries to select the database, again if it can't it stops loading and loads an error message.


SQL Queries:

*:
* in SQL means everything, so instead of using
SELECT `field`, `field2`, `field3` FROM `table` WHERE `condition` = 'constraint';
You would do
SELECT * FROM `table` WHERE `condition` = 'constraint';

%:
% in SQL is a wildcard, say you wanted to find a user with a bad word in their profile, you would use
SELECT `name` FROM `users` WHERE `profile` LIKE '%badword%';

Delete:
This statement will delete that row of fields. The syntax is
DELETE FROM `table` WHERE `field` = 'value';
So if you wanted to delete a user with id #1 the code would be
DELETE FROM `users` WHERE `id` = '1';

Where:
Say you wanted to make a users clicks go up by one, but you don't want everyones clicks to go up by one. You would use WHERE. First you would get some data that is unique to the user (id, name, etc) and use the code
UPDATE `table` SET `clicks` = $clicks WHERE `id` = 1;
Where $clicks is the amount of clicks the user currently has and 1 is the users ID. Don't worry about the UPDATE... that will be explained soon.

Like:
Like is somewhat like the WHERE query in the fact that it's a constraint. The syntax is
SELECT * FROM `table` WHERE `field` LIKE '%constraint%';
For example, if you wanted to find all users with "mike" in their name you would use
SELECT * FROM `users` WHERE `name` LIKE '%mike%';


Putting Info Into Your Database:

Insert:
The first method of doing this is the INSERT statement, the syntax of this is
INSERT INTO `table` (field1, field2) VALUES ('data', 'data2');
Take notice that the order of field names corresponds with the order of info to be inserted.

Update:
UPDATE is used when you want to update existing data. The syntax is
UPDATE `table` SET `field` = 'new data' WHERE `id` = 1;

Now to use these you would have to find a way to run them, once connected to your server you would have the following code

Code:

<?php
    include("connect.php");
    mysql_query("Your Query");
?>

Extracting Info From Your Database:

Select:
This is the only method of getting information out of the database, the syntax is like so
SELECT `field` FROM `table` WHERE `condition` = 'constraint';

To put this to use you would have

Code:

<?php
    include("connect.php");
    $name = mysql_query("SELECT `name` FROM `users` WHERE `id` = '1'");
    echo $name;
?>

Go ahead, try it. You get some funky Resource ID huh. The way to fix this is to put the information you get into an array. To do this you have the following code.

Code:

<?php
    include("connect.php");                                        // connect to your server
    $sql = mysql_query("SELECT * FROM `users` WHERE `id` = '1'");    // put our query in a variable
    $user = mysql_fetch_array($sql);                                // get the info and put it in an array
    /*
    Lets assume for a minute that in the user table you have the fields name, id, and email
    The name is Mike, the ID is 1, and the email is email@domain.com
    */
    echo $user['name'];    // will echo name
    echo $user['id'];        // will echo 1
    echo $user['email'];    // will echo email
?>

Voila! It works! So go ahead and try it out, experiment, build a user login with a database! Now is when you really start learning how an online game works.

Mike 25-02-2005 13:31

Re: Teach Genia How to Program and Win a Prize
 
Hmm, some stuff got removed by the forums.

I'm going to start work on a C Tutorial Whitepaper, and a PHP Tutorial Whitepaper (Both based off these tutorials)


All times are GMT -5. The time now is 15:57.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi