The MySQL reserved are upper cased, all the text in lower case are things
that you can change. This basically creates a table named ‘mytable’ with
two columns; “id” and “name”, id is an INT(eger) and name is a varchar of
max 255 characters.

AUTO_INCREMENT means that id will be automatically incremented if not defined.

Connecting to MySQL from PHPDisclaimer: This is a very basic example, if you are coding for production use please at least look at the function mysql_real_escape_string() or a library
that will handle insertion and such.

The example above inserts a record in MySQL, the id will be automatically set to 1 (for the first row you insert, 2 for the next, etc.). To read this record out from the database again you can do something like this:

Data from MySQL

This piece of code will perform a “SELECT * FROM mytable” query (the * means every column from the table, you could get only the name by doing “SELECT name FROM mytable“.
Since there can be multiple rows we perform a while loop, so while $row gets fed with data from the mysql_fetch_assoc() function it will print the ‘id’ and ‘name’ columns from the table.

Select specific row(s) only (match on name)

Data from MySQL

Now we used the LIKE keyword in MySQL, the percent (%) sign is a wildcard.
This will get every row from the table where name begins with an E.

I had promised myself that I would post every second day, but I didn’t know exactly where to go from what we have built up and I also became engaged, she said yes! So it has been crazy days.
But I’m happy, at least

I am going to continue with showing how to take use of some of the built-in string fuctions. These can be used to manipulate text variables that you might receive from the $_GET and $_POST variables.

Find occurrence of text in strings
You can for example use the function strstr() or stristr() if you want a cast-insensitive matching style.

Have you been playing around with the GET and POST arrays now?
If not, learn about foreach in PHP here.

What can I do now?
You can go ahead and use if to check the contents of variables.
Check this out.

If
The syntax for if goes like this:

if (statement) {
Insert your code here
}

The statement will be checked, and if it is true the code within the { and } will be executed.
Take for example a script where you get a variable from the url, take the previous
example where you used $_GET['name'] to retrieve the ?name= part of the URL.

Now, if your name is John and you want the page to say hello to you but not to anyone else.

The $_GET['name'] == ‘John’ part is called the statement,
you probably understand it but it checks to see if the $_GET array variable
with the key ‘name’ contains a string ‘John’.

You can do similar tests with numbers, but then you won’t need the quotes
like the ones I have around ‘John’.

The example will then be:

Let us say you want to check if the name is NOT John.

You're not my master.

Else
Now, take the example where you output ‘Hi master’, what if you want
to print something to the ones not being the master? You can use else

You're not my master

That’s all for today, I don’t have so much time today so I am going to keep it quite short.
I will get back with a new article in these series in a couple of days, hope I’ll see you back here then!

In my previous article I explained arrays and their use. But have you ever wondered what happens to those variables set in the url? They are contained within an array which is prefilled for your scripts.

The array variables $_GET, $_POST and $_SERVER are some of the interesting ones you can use.

The reserved $_GET variable
An example would be this type of URL.http://www.example.domain/myphpscript.php?name=Espen

To catch the variable ‘name’ you can do this piece of PHP

<?php
echo “Hi ” . $_GET['name'];
?>

This will output “Hi Espen”.

Using the reserved $_POST variable
To get the variables from a form you can use $_POST[‘key‘].
Look at this example:

Create this HTML form, change the path to myhelloscript.php to a place where you test your scripts.

You see? The same result here, only now we’re using POST instead of GET (you can also notice that the URL does not contain the variables).

Looping over an array
Looping over an array can be very usefull in the case where you don’t know what the reference keys are.
Let us say you didn’t know that the key for the name in the previous post example was not called ‘name’, how would you find it?

Here is an example of looping over an array, looping through all the key and value pairs.

<?php
$myarray = array(‘Espen’, ‘John’, ‘Lisa’, ‘Mark’);

foreach ($myarray as $value) {
echo $value . “<br>”;
}
?>

Now we’re saying for each ‘entry’ in $myarray get the entry as $value and do something with it between the { and }
So for each $myarray as $value: echo $value and a ‘<br>’ (to get a newline).

This example will output

Espen
John
Lisa
Mark

Getting the key
But that’s not the key, is it? How do you find the keys?
This example will use the foreach command in another way to get the keys also

An array is a systematic arrangement of objects, usually in rows and columns. Specifically, it may refer to several things.

That’s exactly what it is, it’s a variable (see previous post) containing several values (like a shelf with several compartments) that can be retrieved by a key (the number on the compartment). Array keys are per default a number, but it can also be an associative array with text strings for keys.

So make an array and fill it with data
Here is an example on how to declare an array:

Deleting a value from a PHP array
Several ways to this also, you can use array_pop() to unset the last value in the array.
The unset() function can be on the array value to unset it.
You can set it to null by referring to the key

array_pop($bookshelf);
unset($bookshelf[3]);
$bookshelf[3] = null;

While these are regular arrays, there are other possible ways to use this. A string variable like $name = “Espen”; in my previous article is actually an array of characters. $name[0] will refer to ‘E’, 1 will refer to ‘s’ so on and so forth.

Associative Arrays in PHP
You can use keys which are not numbers in an array, those are called associative arrays.
Here is an example, I bet you understand most of the basics now, and in case NOT – drop me a comment!

In my next article you will get magic behind the array variables $_GET, $_POST and $_SERVER to finally be able to handle user input from forms and every secret of the request. Want to know when I post it? Get Learning by RSS.

I’ve been coding (or scripting if you want) PHP actively for myself for probably 10 years now, over the years I’ve had a lot of questions about it and have done a lot of small favours for people. I would be happy to give away some of my knowledge, so please ask if there is anything.

I am going to teach PHP from basic to advanced, I will try to keep it to one article every other day.Subscribe to my feed if you don’t want to miss out on this chance.

Basic Introduction
What is PHP? It is a scripting language that resides on the server-side as opposed to javascript which is downloaded and ran by the client (which is called client-side).

To be able to execute PHP scripts, you need access to a webserver that runs PHP scripts – there are a lot of alternatives.
I am not going to explain them here, but I am sure it is possible to arrange something if you need a place and is into learning.
It is also possible to run an Apache server locally with PHP.

My First PHP Script
Create a file named first.php and this is the contents

<?php
echo “hello (word)”;
?>

Save this file somewhere on the webserver and try to go to the url.
It will display: hello (world)

ExplanationThe first line is <?php, this tells the webserver that it is about to parse PHP scripting. You need this before every piece PHP script for it to be executed as PHP.

The second line is echo “hello (world)”;, this is actually the PHP script line.
It uses the built-in function ‘echo’ to display a string, the string needs to be contained within quotes.
And notice that the ending of the line is ;, this finishes off the statement.
If you were to forget the ; and had several lines of code the server will continue to feed the rest of the code into the echo function, which would probably not work or give really really unexpected results.

The third line is ?>, this tells the webserver that the PHP script is over.
When it reaches this tag, it will continue to output the rest of the page to the browsing user. This means that you can use PHP in between HTML to generate dynamic responses.

My Second PHP Script
Heck, since it’s the first post and the first script was so easy.. I will show you another script and teach how variables work.
I will move on to more advanced things later, so keep watching if this is way too basic for you. There will be more blood!

Explanation
The first line of PHP script is $name = ‘Espen’;.
The dollar sign ($) indicates that this is a variable, variables are used to contain dynamic information like if you want to display it at several places in your document.

The variable name is actually ‘name’, and it is set to contain anything behind the equal (=).
The string behind the equal sign is “Espen”, notice the ; ending again.
Because it is contained within quotes, PHP automatically identifies it as a string, it will not work without quotes.
I will show some examples between the difference in double and single quotes later.

The second line is $age = 23;.
As before, notice the dollar sign and also notice the ; ending.
Because age is an integer, you don’t need quotes. If you use quotes around the age like ’23′, PHP will identify it as a string and doing calculations on it can fail.

The third line is $twice = $age * 2;
This sets the variable named $twice to contain whatever is in the variable $age and multiply it with two.
We know from before that $age is set to 23, so this will yield a result of 46. $twice now contains 46.

The fourth line is:
echo “My name is ” . $name . ” and my age is ” . $age . “, if I had lived twice as long I’d be ” . $twice;

One of the methods of combining variables into another is the usage of the period character (.) There are other and better ways to do it, but that may be to complicated for a beginner for now.
Some if you may relate this to javascript where they use + to combine variables.
So this actually feeds a lot of parts of information to the echo function to display it:
“My name is ” is the first part, the next variable will be printed right after it – so notice the white space before the last qutoe.
Then the period character is used to append the variable $name to it.
The string ” and my age is ” is appended by . again, notice white spaces before and after the string.
You probably understand the rest of it now.Notice that any white space added that is not contained within quotes will not be displayed either.

If anything is unclear or completely wrong, feel free to drop me a comment!
More learning is coming: Subscribe to my feed.

Now, there are some changes that has to made to make haXe like the API
I generated the working sources with a newer version of haXe, and applied the changes to it.
This makes the patchfile a bit larger than it had to be, because I had to make a patch against my haxe 1.19-generated hxclasses/ folder.
Anyways, this works and it is against the 1.9 file from Google.

Voila! Your first Google Flash Map, fresh from the oven.. the filename is map.swf
Try to include this on a webpage, check the source here if you don’t know how to do it.
Your API key should be in the flashvars when applying the flash object on a webpage.
&ltparam name=”flashvars” value=”key=key”/&gt

Here is my result, I messed up the width/height of my app, but it works:

Mashups. To be able to think creatively about it you must know which sites offer an API. And then; what kind of data you can expect to be able to access. Here is a list of websites which offer an API. Some are the most popular, but you will find a little golden egg or two in this list.

Facebook have an API, you can use most of the data users have stored at facebook, like friends, profile info, photos.API Docs:Facebook API Documentation

With the bit.ly API you can expand and shorten URL’s, then gather statistics about traffic on those specific short URL’s.
A lot of Twitter users post links shortened with bit.ly, so it would also be possible to keep track of what’s popular by the use of this API.API Docs:Bit.ly API Documentation