Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Tutorials and Tips

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 01-16-2011, 10:13 PM
Kaeliah's Avatar
Kaeliah Kaeliah is offline
Premium Member
 
Join Date: Sep 2010
Location: Pennsylvania, United States
Posts: 485
Gender: Female
Credits: 38,443
Kaeliah will become famous soon enough
Send a message via AIM to Kaeliah Send a message via MSN to Kaeliah
Default Basic PHP & MySQL Tutorial

This guide is for those who know nothing or little about PHP and MySQL.

1. Hosts, Databases and Other Info

Places like Freewebs and Geocities don't support databases and PHP files, so you have to get a little creative. There are plenty of free and paid hosting out there for you to use that do support PHP/MySQL but finding them can be a hassle. So I'll give you some personal recommendations.

Free:
www.000webhost.com
Paid:
www.webhostingpad.com
www.godaddy.com

WBH is the only paid host I've ever used but they're extremely helpful and have tons of features for a very decent price. Anyways, my suggestion for free hosts is 00webhost but I would NOT suggest Byethost because they don't support a couple things that are required. There are others out there, these are simply the ones I'm familiar with.

Now, once you've got a host down lets cover some other basics, Databases! A database is where all the information for your site will be stored. It'll be stored in detailed tables that can be added to, deleted from and updated at pretty much any point in time. Web Hosts will have specific directions as to how to set up a database, and make sure you copy down any information, such as the Database Name, the Database User and Password and so forth. This will come in handy when connecting to the Database. Now the Rusnak Script comes with a pre-installation, so you input the information where it goes and then you'll be able to connect to your database automatically as it's already coded into each page(Just be sure to use the 'blank.php' template when making new pages). If people ask I'll show how to code a connect script but for now lets stay simple.

Once connected to a database, you can access it by coding MySQL 'Queries'. Basically your telling the database to get, add, delete or update information. There are many useful functions that can be done but for now those are the basics.

When using PHP, similar to HTML you must save it as a different document type. When saving an HTML file, you save it as .html, although if your saving a file with php you have to save it as .php. You can edit PHP code in Windows Notepad, or download a better editor. My personal favorite is Notepad++, although if you want a debugger(a check that finds mistakes in the code) PHP Designer 2007 is a pretty good program. The best way to learn any code, including PHP is to get in there and figure out what's going on with a script and try to change a couple things. So let's get to scripting.

2. PHP Basics

Before we get into MySQL let's get into some PHP basics. A quick vocab word: Syntax. Syntax refers to the grammar and structure of the code. If you have a syntax error, it's probably because you forgot a ';' or a '}' in the code. The first things you should know about PHP is the tags. You have to use these tags within a PHP file in order for the PHP to work.

PHP Code:
<?php
 PHP CODE HERE
?>
Simple enough right? You can make the entire page like this or just blocks of the page. PHP is convenient in that you can make the page mostly HTML with a little PHP or the other way around. MA sites are mostly PHP with a little HTML. Next thing is variable. If your familiar with Algebra or higher then you'll already know what a variable is. Variable comes from the word vary, which means it changes, it's not always the same. Variables in PHP always start with a $ followed by a letter. A variable starting with a $_ or a $4 will not work! Some examples of variables:

PHP Code:
$money
$pet_id
$total
$tax2 
You can set a value for a variable very easily. See a variable as a sort of bottle, with a little message inside. It can only hold one message, but it holds it none the less. If you say 'okay this variable equals 5,' then the little message inside the bottle is going to say 5

PHP Code:
$money 5
Note the syntax here. I used an equal sign to say that the variable now represents the number 5, then ended the line in a semi-colon. The semi-colon signifies the end of what that variable represents or is equal to. You can also store text in a variable.

PHP Code:
$welcome "Welcome to My Site!"
Note that the message is in quotation marks. That's so the computer reads it as text instead of code, and then the semi-colon to end the statement. Now what can you do with these variables? A whole lot! First let's use some operators. The four basic ones; multiplication, addition, division and subtraction. Take a look at the following bit of code.

PHP Code:
$num1 4;
$num2 8;

$num3 $num1 $num2
$num3 would then be set to the value of 4 + 8 which is 12. Replace the addition sign with a subtraction and the variable $num3 would be -4. And so on and so forth through the other two operators. There are several operators that work slightly differently.

PHP Code:
$num1 5;
$num1++; 
The code above is saying that the variable equals five, and then the variable is incremented(added to) by 1. So then the variable would equal six. This also works for '--'.

Alright now that we know some basic PHP code and how to write it, lets move on to something a little more useful. If-Else Statements.

PHP Code:
if($money == 5){
echo 
"You have 5!"; }
else {
echo 
"You don't have 5!"; } 
What can you gather from the code above on your own? If we translate the script into English as best we can, it says 'If the variable money is equal to 5, then print on the page "You have 5!" If it does not equal five, print on the page "You don't have 5!"' You start the code off with the if(). In the parentheses you need your requirement. For example if you were to only allow users with enough money to purchase an adopt, then you would check to see if they have enough. If they have enough, they can purchase, otherwise they get an error message. The else is not required however it is suggested in most cases. The other option is 'else if()' meaning there is a secondary check if the first one is false. So if they don't have EXACTLY the amount they need to purchase the pet, do they have more? If they do you'd still allow them to purchase the pet so you need a second check. the else then represents a situation that did not fall into any of the previous categories. The requirement itself has a certain coding. '==' means 'is equal to'. Using only one equals sign will mess up your code. Other options to use are '!=' which is not equal to, <, >, <=, and >=. Respectively they are less than, greater than, less than or equal to and greater than or equal to. You can also use these checks to identify text as well, whether something says yes, no or is enabled or whatever. Next come the brackets '{ }' Anything within those brackets will be done if the requirement is satisfied. If the requirement is not satisfied, the code within those brackets will not be used. Make sure to always have a closing bracket!! You also may notice something new: echo. Echo simply means print onto the page whatever is in the quotations.

For organization purposes, there is something called comment lines, and comment blocks. Most scripts only bother with comment lines. When you get a code that's thousands of lines long, comments are very helpful for finding what your looking for. Comment lines can say a simple statement about the code following it.

PHP Code:
// Start log in check
$fix 5;  // This variable equals 5 
These lines can be placed just about anywhere including after code. The double slashes '//' will tell the computer to disregard the rest of the line. Comment blocks are multi-line comments used for heading of specific sections of code or copyrights and the like.

PHP Code:
/* 
This is
A
Comment
Block
*/ 
Comment blocks can be as many lines as you want and the slash stars '/*' and '*/' tell the computer to disregard anything in between them.

Congratulations, you know some PHP basics!

3. MySQL Basics

MySQL goes hand in hand with PHP quite well. While there are different ways of running a query, this is the one I'm most familiar with. These queries can be written directly into the PHP code like this,

PHP Code:
mysql_query(); 
Now depending on what you want to do within the query, we fill in the parentheses. Lets start with a Select From.

PHP Code:
mysql_query("SELECT * FROM tablename"); 
That code will select every entry from 'tablename' in the database your connected to. This would be good if you were listing every user for example. However sometimes you want to select entries that are a little more specific. Say you want to select all the pets of one type.

PHP Code:
mysql_query("SELECT * FROM tablename WHERE type='cat'"); 
Let's take a look now. WHERE adds a condition. So it will only select entries from table 'tablename' when one of the fields, in this case type, contains the work cat.You can add multiple requirements by placing an AND in between. for example,

PHP Code:
mysql_query("SELECT * FROM tablename WHERE type='cat' AND owner='love'"); 
In this case, the query will ONLY select data entries that satisfy both requirements. Now lets move on to another query type. Update is a way you can change what is in a specific field or fields of a data entry.

PHP Code:
mysql_query("UPDATE tablename SET canadopt='yes'"); 
In the above query, it's saying update table 'tablename' and set field 'canadopt' to yes for all entries. This is great if your trying to change something sweeping, but sometimes you only want to change one little thing. In that case you want to use something like this,

PHP Code:
mysql_query("UPDATE tablename SET canadopt='yes' WHERE username='Chibi'"); 
This query will only set the field 'canadopt' to yes when in the same data entry, the username is equal to 'Chibi'. One more thing we'll cover in the MySQL basics area is using variables in queries. Yes, the PHP variables you learned earlier. Here's an example,

PHP Code:
mysql_query("SELECT * FROM tablename WHERE type='cat' AND owner='".$loggedinname."'"); 
Alright so what do we notice here? You've probably noticed earlier that the actual thing your looking for in the data entry is in ' ' marks. So now within those ' ' marks, to put a variable we place the full quotation marks " ". Then within those marks, we use to periods. Within the periods you put the variable, starting with the $ sign. Personally I prefer this way because it pops out more in the coding and is colored differently in certain programs. You can also use this method to place variables in PHP strings, but we're not going there in this tutorial.


And there you have it. The basics of PHP and MySQL!

Last edited by Kaeliah; 01-30-2011 at 12:45 PM.
Reply With Quote
  #2  
Old 01-16-2011, 11:39 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 487,983
Hall of Famer is on a distinguished road
Default

Very helpful tutorials Kaeliah, I know you will make one. This should be good for aspiring PHP learners, lets hope we will have more PHP programmers in future.

*stickied*

Hall of Famer
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #3  
Old 01-16-2011, 11:44 PM
Kaeliah's Avatar
Kaeliah Kaeliah is offline
Premium Member
 
Join Date: Sep 2010
Location: Pennsylvania, United States
Posts: 485
Gender: Female
Credits: 38,443
Kaeliah will become famous soon enough
Send a message via AIM to Kaeliah Send a message via MSN to Kaeliah
Default

Thanks Famer.
Reply With Quote
  #4  
Old 01-16-2011, 11:48 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 487,983
Hall of Famer is on a distinguished road
Default

You are very welcome. I will wait till you finish the loop tutorials and then post my tutorials for arrays.
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #5  
Old 01-16-2011, 11:58 PM
Kaeliah's Avatar
Kaeliah Kaeliah is offline
Premium Member
 
Join Date: Sep 2010
Location: Pennsylvania, United States
Posts: 485
Gender: Female
Credits: 38,443
Kaeliah will become famous soon enough
Send a message via AIM to Kaeliah Send a message via MSN to Kaeliah
Default

Good because I HATE arrays.

Reply With Quote
  #6  
Old 01-17-2011, 12:00 AM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 487,983
Hall of Famer is on a distinguished road
Default

lol...

Actually I dont see arrays or classes being used in RA, so guess they are not really required to work on modifications of this script. XD
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #7  
Old 01-18-2011, 06:24 AM
Sasoragon's Avatar
Sasoragon Sasoragon is offline
Beginniner Coder :D
 
Join Date: Jan 2011
Location: In the Myadopts Support Forums
Posts: 29
Gender: Female
Credits: 7,014
Sasoragon is on a distinguished road
Default

...Kae, this is brilliant.

This tutorial REALLY helps. I couldn't figure ANYTHING out about MySQL before now. (Mostly because w3school's MySQL tutorial is a strange-ish blur)
Reply With Quote
  #8  
Old 01-18-2011, 09:44 AM
Kaeliah's Avatar
Kaeliah Kaeliah is offline
Premium Member
 
Join Date: Sep 2010
Location: Pennsylvania, United States
Posts: 485
Gender: Female
Credits: 38,443
Kaeliah will become famous soon enough
Send a message via AIM to Kaeliah Send a message via MSN to Kaeliah
Default

Yay I'm glad I could help. And ADORABLE avatar!!!
Reply With Quote
  #9  
Old 03-23-2011, 07:50 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 487,983
Hall of Famer is on a distinguished road
Default

Found a very interesting article about Mysql commands in PHP, looks like it will help a lot:
http://www.createafreewebsite.net/phpmysql/alter.html
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic Browser Game Tutorial using Smarty and Bootstrap Kesstryl Programming and Game Development 3 10-29-2014 11:15 PM
I need basic coding help Moontides Feedback and Suggestions 23 05-28-2014 02:54 PM
Intermediate PHP & MySQL Tutorial I Kaeliah Tutorials and Tips 4 01-31-2011 04:20 PM
PHP/MySQL Tutorial HIddenPanda Tutorials and Tips 0 01-27-2011 05:30 PM
My BASIC programs SleepWalker Other Chat 0 04-05-2010 09:37 AM


All times are GMT -5. The time now is 11:05 AM.

Currently Active Users: 687 (0 members and 687 guests)
Threads: 4,081, Posts: 32,031, Members: 2,016
Welcome to our newest members, jolob.
BETA





What's New?

What's Hot?

What's Popular?


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636