Handy PHP SQL Commands

References used for these notes include:

  1. http://www.w3schools.com


Introduction

You can access databases using 2 different PHP packages:

  1. MySQLi extension (the "i" stands for improved)
  2. PDO (PHP Data Objects)

Prior to PHP 5, PHP used the MySQL extension package (note the lack of the "i"), but this package was deprecated in 2012. You may still see commands such as "mysql_query(...)" rather than "mysqli_query(...)". Such commands are using the old extension package. If you are maintaining such code, you should probably consider converting the commands to the newer mysqli package.

The MySQLi package has both an object-oriented and a procedural API. In this course we will only consider the procedural API, except for SQL prepared statements. The PDO package allows PHP to connect to databases other than MySQL, but the syntax is more complicated. In this course we will only consider the MySQLi package.


Establishing Connections to the MySQL Server


Executing Queries

Prepared Sql Statements


Extracting Information from Prepared Statements

  1. mysqli_stmt_num_rows($stmt): returns the number of rows returned by a prepared statement that executes a SELECT statement. $stmt is the resource returned by the mysqli_prepare statement.
  2. mysqli_stmt_affected_rows($stmt): returns the number of rows affected by an UPDATE, INSERT, or DELETE prepared statement. $stmt is the resource returned by the mysqli_prepare statement.

Extracting Information from Non-Prepared Statements