PHP MySQL Insert Data

Insert Data Into MySQL Using MySQLi and PDO

After a database and a table have been created, we can start adding data in them.

Here are some syntax rules to follow:

  • The SQL query must be quoted in PHP
  • String values inside the SQL query must be quoted
  • Numeric values must not be quoted
  • The word NULL must not be quoted

The INSERT INTO statement is used to add new records to a MySQL table:

INSERT INTO table_name (column1, column2, column3,…)
VALUES (value1, value2, value3,…)

Continue reading PHP MySQL Insert Data

PHP MySQL Create Table

A database table has its own unique name and consists of columns and rows.

Create a MySQL Table Using MySQLi and PDO

The CREATE TABLE statement is used to create a table in MySQL.

We will create a table named “MyGuests”, with five columns: “id”, “firstname”, “lastname”, “email” and “reg_date”:

CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

Notes on the table above :

The data type specifies what type of data the column can hold. For a complete reference of all the available data types, go to our Data Types reference. Continue reading PHP MySQL Create Table

PHP Create a MySQL Database

A database consists of one or more tables.

You will need special CREATE privileges to create or to delete a MySQL database.

Create a MySQL Database Using MySQLi and PDO

The CREATE DATABASE statement is used to create a database in MySQL. Continue reading PHP Create a MySQL Database

PHP Connect to MySQL

PHP 5 and later can work with a MySQL database using:

  • MySQLi extension (the “i” stands for improved)
  • PDO (PHP Data Objects)

Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012.

Continue reading PHP Connect to MySQL

PHP MySQL Database

With PHP, you can connect to and manipulate databases.

MySQL is the most popular database system used with PHP.

What is MySQL?

  • MySQL is a database system used on the web
  • MySQL is a database system that runs on a server
  • MySQL is ideal for both small and large applications
  • MySQL is very fast, reliable, and easy to use
  • MySQL uses standard SQL
  • MySQL compiles on a number of platforms
  • MySQL is free to download and use
  • MySQL is developed, distributed, and supported by Oracle Corporation
  • MySQL is named after co-founder Monty Widenius’s daughter: My

The data in a MySQL database are stored in tables. A table is a collection of related data, and it consists of columns and rows. Continue reading PHP MySQL Database

PHP Iterables

PHP – What is an Iterable?

An iterable is any value which can be looped through with a foreach() loop.

The iterable pseudo-type was introduced in PHP 7.1, and it can be used as a data type for function arguments and function return values.


Continue reading PHP Iterables

PHP Namespaces

PHP Namespaces

Namespaces are qualifiers that solve two different problems:

  1. They allow for better organization by grouping classes that work together to perform a task
  2. They allow the same name to be used for more than one class

For example, you may have a set of classes which describe an HTML table, such as Table, Row and Cell while also having another set of classes to describe furniture, such as Table, Chair and Bed. Namespaces can be used to organize the classes into two different groups while also preventing the two classes Table and Table from being mixed up.


Continue reading PHP Namespaces

PHP OOP – Static Properties

PHP – Static Properties

Static properties can be called directly – without creating an instance of a class.

Static properties are declared with the static keyword:

Syntax

<?php
class ClassName {
  public static $staticProp = "iampsp";
}
?>

To access a static property use the class name, double colon (::), and the property name: Continue reading PHP OOP – Static Properties

PHP OOP – Static Methods

PHP – Static Methods

Static methods can be called directly – without creating an instance of the class first.

Static methods are declared with the static keyword:

Syntax

<?php
class ClassName {
  public static function staticMethod() {
    echo "Hello World!";
  }
}
?>

To access a static method use the class name, double colon (::), and the method name: Continue reading PHP OOP – Static Methods

PHP OOP – Traits

PHP – What are Traits?

PHP only supports single inheritance: a child class can inherit only from one single parent.

So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.

Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected). Continue reading PHP OOP – Traits