php

PHP – MYSQL – Create a Table

PHP – MYSQL – Create a Table

1. You can create a table using phpMyAdmin

Blue Host users:

CPanel> phpMyAdmin> Enter with User name and password

LEFT COLUMN> you will see ‘Create Table’ button

2. You can create a table using PHP 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!";
  }
  
// Create table
$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";

// Execute query
if (mysqli_query($con,$sql))
  {
  echo "Table persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error($con);
  }
?>

Execute the script and check the Database with phpMyAdmin, you will see on TOP LEFT a small icon with a GREEN ARROW that will refreshes the table list.

Click over the new Table ‘Persons’, you must see:

– a table named “Persons”
– three columns: “FirstName” – “LastName” – “Age”

AN ERROR MESSAGE: Nessun indice definito!

Ogni tabella necessita di un indice UNIVOCO per identificare le righe in una tabella.
Per creare una tabella con un indice possiamo utilizzare il seguente codice:

$sql = "CREATE TABLE Persons 
(
PID INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(PID),
FirstName CHAR(15),
LastName CHAR(15),
Age INT
)";

Eliminiamo la tabella da phpMyAdmin:
phpMyAdmin> In ALTO localhost> lucedigi_testphp> spuntiamo la tabella> ‘Elimina’ il messaggio di conferma sarà “Drop Table…”> OK

Eseguiamo lo 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!";
  }
  
// Create table with an INDEX
// PID is: Primary Key ID
// it is an INT integer number
// it is not NULL
// AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added.
$sql = "CREATE TABLE Persons 
(
PID INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(PID),
FirstName CHAR(15),
LastName CHAR(15),
Age INT
)";

// Execute query
if (mysqli_query($con,$sql))
  {
  echo "Table Persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error($con);
  }
?>

Controllare il database with phpMyAdmin: il messaggio di errore è scomparso, sotto è stata aggiunta la voce ‘Indice’ dove la chiave primaria è il ‘Campo’ ‘PID’

By |MySQL, PHP, Web Design|Commenti disabilitati su PHP – MYSQL – Create a Table

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