PHP – MYSQL – Open and Close a connection to the MySQL server

PHP – MYSQL – Open and Close a connection to the MySQL server

1. Create a Data Base and assign a User

Blue Host users:

CPanel> Database Tools> MySQL databases>
– Create a Database
– Create a User
– Assign User to Database

2. Manage the DB

Blue Host users:

CPanel> phpMyAdmin> Enter with User name and password

LEFT COLUMN> you will see your new empty Database.

A Database is a data structure with tables, every table has rows and columns.

3. Write PHP code

Open a connection, it will be closed automatically when the script ends

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }
?>

Statement: mysqli_connect(host,username,password,dbname);

NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost

To close a connection using script:

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }

// Close connection
mysqli_close($con); 
echo "Connection Closed";
?>

NOTICE: PHP function – mysqli_close() –

By |MySQL|Commenti disabilitati su PHP – MYSQL – Open and Close a connection to the MySQL server

PHP – Global Variables – Superglobals

PHP – Global Variables – Superglobals

Superglobals are variables that are always available in all scopes.
Superglobals always accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special.

The PHP superglobal variables are:

$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

$GLOBAL

$GLOBAL is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods).

In the next example, since z is a variable present within the $GLOBALS array, it is also accessible form outside the function!

<?php 

$x = 75;
$y = 25; 

function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

addition();
echo $z; // Output: 100

?>
By |PHP, Web Design|Commenti disabilitati su PHP – Global Variables – Superglobals

PHP Arrays

PHP Arrays

An array it is a single variables that stores multiple values.

Two dimensional Array

<?php

// ---------------
// Basic Structure
// ---------------
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
//Output: I like Volvo, BMW and Toyota.

// ---------------
// Associative array
// ---------------

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
// Output: Peter is 35 years old

// ---------------
// Count - Get the lenght of an array
// ---------------
$cars=array("Volvo","BMW","Toyota");
echo count($cars); // Output: 3

// ---------------
// Loop through and indexed array
// ---------------
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)
  {
  echo $cars[$x];
  echo "<br>";
  }
// Output: Volvo BMW Toyota

// ---------------
// Loop through an Associative array
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

foreach($age as $x=>$x_value)
  {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
  }
// Output: Key=Peter, Value=35 Key=Ben, Value=37 Key=Joe, Value=43

?>

Sorting Arrays (Ordinamento degli Array)

<?php

// ---------------
// sort() - sort arrays in ascending order
// ---------------
$cars=array("Volvo","BMW","Toyota");
sort($cars);

$clength=count($cars);
for($x=0;$x<$clength;$x++)
   {
   echo $cars[$x];
   echo "<br>";
   } 
// Output: BMW Toyota Volvo

// ---------------
// rsort() - sort arrays in descending order
// ---------------
$cars=array("Volvo","BMW","Toyota");
rsort($cars);

$clength=count($cars);
for($x=0;$x<$clength;$x++)
   {
   echo $cars[$x];
   echo "<br>";
   }
// Output: Volvo Totota BMW

// ---------------
// asort() - sort associative arrays in ascending order, according to the value (NOT THE KEY - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
// Output: Peter Ben Joe

// ---------------
// arsort() - sort associative arrays in descending order, according to the value (NOT THE KEY - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
Output: Joe Ben Peter

// ---------------
// ksort() - sort associative arrays in ascending order, according to the key (NOT VALUE!!! - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
// Output: Ben Joe Peter

// ---------------
// krsort() - sort associative arrays in descending order, according to the key (NOT VALUE!!! - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
Output: Peter Joe Ben

?>

Multidimensional Array

A multidimensional array is an array containing one or more arrays.

<?php
$families = array   // Multidimensional Array
  (
  "Griffin"=>array  // First Array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array // Second Array
  (
  "Glenn"
  ),
  "Brown"=>array    // Third Array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
  );
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
// Statement: multidimensional array name['first array name'][first array index]
// Output: Is Megan a part of the Griffin family?
?>
By |PHP, Web Design|Commenti disabilitati su PHP Arrays

PHP – Classes – Objects

PHP – Classes – Objects

La classe è un contenitore per variabili e funzioni, ad esempio ‘class Car’ contiene variabili e funzioni, è un buon modo per raggruppare cose che funzionano insieme.

An object is a data type which stores data and information on how to process that data.

In PHP, an object must be explicitly declared.

First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods.

We then define the data type in the object class, and then we use the data type in instances of that class.

<?php
class Car // first you HAVE TO declare the object
{
  var $color;
  function Car($color="green") 
  {
    $this->color = $color;
  }
  function what_color() 
  {
    return $this->color;
  }
}
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Classes – Objects

PHP – Functions

PHP – Functions

Remember that function names are case-insensitive

Basic Structure

<?php
function writeMsg()
{
echo "Hello World!";
}

writeMsg(); // call the function and output: Hello World!
?>

Arguments

Variables can be passed to functions through arguments.

<?php

// The argument is: $fname 
function familyName($fname)
{
echo "$fname Tonin<br>";
}

familyName("Andrea");    // Output: Andrea Tonin
familyName("Erica");     // Output: Erica Tonin
familyName("Antonio");   // Output: Antonio Tonin
familyName("Maria");     // Output: Maria Tonin
familyName("Alice");     // Output: Alice Tonin

?>
<?php
function familyName($fname,$year)
{
echo "$fname Tonin. Born in $year <br>";
}

familyName("Andrea","1974");    // Output: Andrea Tonin. Born in 1974 
familyName("Erica","1975");     // Output: Erica Tonin. Born in 1975 
familyName("Serafina","1954");  // Output: Serafina Tonin. Born in 1954 
?>
<?php
function setHeight($minheight=50)
{
echo "The height is : $minheight <br>";
}

setHeight(350); // Output: The height is : 350 
setHeight();    // Output: The height is : 50   -> default value
setHeight(135); // Output: The height is : 135
setHeight(80);  // Output: The height is : 80 
?>
<?php
function sum($x,$y)
{
$z=$x+$y;
return $z;
}

echo "5 + 10 = " . sum(5,10) . "<br>"; // Output: 5 + 10 = 15
echo "7 + 13 = " . sum(7,13) . "<br>"; // Output: 7 + 13 = 20
echo "2 + 4 = " . sum(2,4);            // Output: 2 + 4 = 6
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Functions