Posts related to PHP

How to connect to a MySQL database with PHP (Using MySQLi)

(FYI: this example assumes you know some basic SQL)

There are three main ways that you connect to a MySQL database using PHP: the original MySQL extension (function based), MySQL Improved (MySQLi – Object-oriented) and PHP Data Objects (PDO, Object-oriented). This little piece will be about using MySQLi to insert, update and get data from a MySQL database.

Connecting to the database

To connect to a database, we have to create a “link” that we will pass all of our queries through later on. So, to start all we need is:

The $connection variable will become our “link” to the database from now on. We’ll use it to pass through all of our queries. Within the mysqli_connect function, you’ll have to replace the appropriate string with the MySQL host, the database user’s username and password and the database you’ll be accessing.

read more

PHP – Variables, Types and Operators

What is a variable?A variable is like a custom-made box. It can only hold one thing at a time and you

should specify the type of thing you want to put in it when it’s being made.Each variable starts with a dollar sign ($) in PHP and can hold various types of data (e.g: strings (‘Hello’), integers (5), floats (3.141592), arrays ({1,2,3})). Each variable can only hold one value in it at a time but the value can be changed later on.

Explicit Type Conversion/Type Declaration

When first declaring a variable, you can choose to tell PHP what type you want to store in it and will then convert the value you give it to match.

Implicit Type Conversion

PHP will automagically convert variables into the relevant type on its own without you needing to declare its type. It’s always best to think clearly of the type you need and tell PHP that you want that type to ensure everything works the way you intend it to.

read more