PHP MySql Connection


How to use PHP to connect to MySQL database

<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "database";
$connect = mysql_connect($host, $user, $pass) or die("Kunde inte ansluta till MySQL ". mysql_error());
mysql_select_db($db);
?>


Maybe you want to make a check the connection is good.

// Check connection
if (!$connect) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Object-Oriented Programming PHP

First, we create PHP called function.php, in there we can store all the functions (we are going to create only one)
So when that’s done we are going to create index.php, we will go into that file later in this post. Now back to function.php

function.php

<?php
session_start();
function Welcome()
{
	print "Welcome to <h2>PHP with 08Gamer</h2><br>";
}
?>

When function.php is done, jump over to index.php

index.php

<?php
session_start();
include ('funkcion.php'); 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>08Gamer</title>
</head>
<body>
<?php
$oopfunktion->Welcome();
?>
</body>
</html>

So with include (‘function.php’); and $oopfunktion->Welcome();
you can you reuse function very easy and don’t type endless of rows over and over again if they are going to work the same on more the one place.

PHP Arrays

Hi, today will go through two ways to indexing an array:
The index can be assigned automatically (index always starts at 0), like this:

$coding = array("PHP", "HTML", "XML");


or the index can be assigned manually (remember index always starts at 0 even if you start with 1)

$coding[0] = "PHP";
$coding[1] = "HTML";
$coding [2] = "XML";


Here an example of how you can use it

<?php
$coding = array("PHP", "HTML", "XML");
echo "I want to create a webpage based on " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

PHP Strings

Let’s take a look at PHP string and how it can be used, we will go through three different ways you can use it and with three examples.

First PHP String is strlen, with this one we can count how many keystrokes there is (its even count’s spaces) So do you give it a value so will in count it.
So in this example, we will feed strlen with count keystrokes.

<?php
echo strlen("count keystrokes");
?>

The output will be 16

Second PHP String is str_word_count as the name says, it will count word the given value contains. So in this example,
we will reuse the work count keystrokes.

//Count the words
<?php
echo str_word_count("count keystrokes"); // outputs 2
?>

The output will be 2

In our third and last example of PHP String, we will use strrev.
This turns on the whole value the end becomes first and first becomes last

//flips the words
<?php
echo strrev("count keystrokes"); // outputs !dlrow olleH
?>

The output will be !dlrow olleH

And that’s it for this time, hope you enjoy post PHP Strings.

PHP While loop

Hi, today we are going to show how to create a while loop.
That will be counted until it fills its purpose.

In this example, we create a variable count with value 1.
Now are going to say to it repeatedly until a value is <= (larger or equal) with five.
For every turn count will grow with 1 ($count++)



<?php
$count = 1;

while($x <= 5) {
    echo "The number is: $count <br>";
    $count++;
}
?>

PHP Switch

So you want to have checked, you have a value in our case $i, we want to check what i$ content compare to some values if it’s equal with something of value I want to compare with, we want something to happen.

<?php
$i = 1
switch ($i) 
{
     case 0:
     echo "i equals 0";
     break;

    case 1: #this will be the correct one
    echo "i equals 1";
    break;
 
   case 2:
   echo "i equals 2";
   break;
}
?>

PHP Page Check in header

Easy why make session check on the visitor to prevent them to reach certain sites without reach for example index.php as in this code example

function YouHaveNothingToDoHere() 
   		{
   			if($_SESSION['Titel'] == null || $_SESSION['Titel'] == 0)
   			{
   				header("Location: index.php");			
   			}
   		}

But make sure that you give a value to $_SESSION[‘Titel’] on your index page or else this can be a bit tricky to visit your site.

The best place to perform this check is in the header so check comes before the rest of the page will be loaded. With header(“Location: index.php”);
you can redirect user that does not belong on that page or “who does not meet any desired requirement”.

PHP IF and ELSEIF

What would the coding be without question?
IF and ELSEIF, that would be hard to ask a question and say if that wrong does this. Here we have an example where we have a $i that will contain value 1.
And question coding that will check

if i = 0
if not that is it 1
if not that is it 2

<?php
$i = 1;

if ($i == 0) 
{
    echo "i equals 0";
} 
elseif ($i == 1) 
 {
   echo "i equals 1";
  } 
  elseif ($i == 2) 
  {
  echo "i equals 2";
  }
<?


Here we have almost the same code with a small difference
we are using else instead of elseif.

now it will ask
if i = 0
else it is what it is

Se it like yes or no question
Is it 0
No, then it is $i

<?php
$i = 1;

if ($i == 0) 
{
    echo "i equals 0";
} 
else
 {
   echo "i equals " + $i;
  }
<?

function php

Let learn how to create a function, a very good way to reuse action or command and don’t need to retype it on every page you do.

In this example, you could use it if someone does something wrong,
and want to empty all $_Session values and send them to a specific page that informs the user what they have done is wrong (wrong.php has to be created too for this to work)

this example you can call main.php

function ReturnTOWrong()
{
   unset($_SESSION['Value']); #It will empty $_Session
   header("Location: wrong.php");	
   #When thats done user will forward to wrong.php
}

so now it’s up to you to let your imagination flow how you want to design your function

PHP Destroy

So you want to destroy values that stored in variables
or just destroy it all with session_destroy();


Create Session

<?php
session_start();
?>

Destroy Session


<?php
// remove all session variables
session_unset();


// destroy the session
session_destroy();
?>