PHP – MySQL – Delete Records

PHP – MySQL – Delete Records

Statement:

DELETE FROM table_name
WHERE some_column = some_value

<?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();
  }

// Delete records START
mysqli_query($con,"DELETE FROM Persons WHERE LastName='Griffin'");
// Delete records END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin

BEFORE

mysql-0006

AFTER

mysql-0007

By |MySQL, PHP|Commenti disabilitati su PHP – MySQL – Delete Records

PHP – MySQL – Update Records

PHP – MySQL – Update Records

Syntax:

UPDATE table_name
SET column1=value, column2=value2,…
WHERE some_column=some_value

<?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();
  }

// Update records START
// Set the Age of Peter Griffin to 36
mysqli_query($con,"UPDATE Persons SET Age=36
WHERE FirstName='Peter' AND LastName='Griffin'");
// Update records END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin

BEFORE:

mysql-0004

AFTER:

mysql-0006

By |MySQL, PHP|Commenti disabilitati su PHP – MySQL – Update Records

PHP – MySQL – Order By Keyword

PHP – MySQL – Order By Keyword

Statement:

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

<?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();
  }

// Order records START
$result = mysqli_query($con,"SELECT * FROM Persons ORDER BY age");

while($row = mysqli_fetch_array($result))
  {
  echo $row['FirstName'];
  echo " " . $row['LastName'];
  echo " " . $row['Age'];
  echo "<br>";
  }
// Order records END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin:

mysql-0004

The result:

Glenn Quagmire 33
Peter Griffin 35
Andrea Tonin 39

By |MySQL, PHP|Commenti disabilitati su PHP – MySQL – Order By Keyword

PHP – MySQL – Filter Results

PHP – MySQL – Filter Results

Statement:

SELECT column_name(s)
FROM table_name
WHERE column_name operator value

<?php
// Create connection
// Statement: mysqli_connect(host,lucedigi_user,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();
  }

// Filter records START
$result = mysqli_query($con,"SELECT * FROM Persons
WHERE FirstName='Peter'");

while($row = mysqli_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br>";
  }
// Filter records END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin

mysql-0004

The result:

Peter Griffin

By |MySQL, PHP|Commenti disabilitati su PHP – MySQL – Filter Results

PHP – MySQL – Display Table Content

PHP – MySQL – Display Table Content

Simple

Statement:
SELECT column_name(s)
FROM table_name

<?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();
  }

// SELECT asterisco (tutti i dati) dalla tabella Persons - START
// inserisco i dati nella variabile $result
$result = mysqli_query($con,"SELECT * FROM Persons");

// Restituisce il set di record come un array
// ad ogni chiamata viene restituita la riga successiva
while($row = mysqli_fetch_array($result))
  {
  // Visualizza a video i dati
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br>";
  }
// SELECT asterisco (tutti i dati) dalla tabella Persons - END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

Display the Result in an HTML Table

Statement:
SELECT column_name(s)
FROM table_name

<?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();
  }

// Display the Result in an HTML Table - START  

// SELECT asterisco (tutti i dati) dalla tabella Persons
// inserisco i dati nella variabile $result
$result = mysqli_query($con,"SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
// Display the Result in an HTML Table - END 

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

PhpMyAdmin:

mysql-0006

Result:

mysql-0005

By |MySQL, PHP|Commenti disabilitati su PHP – MySQL – Display Table Content