Show Menu
Cheatography

PHP MySQLi Procedural Cheat Sheet (DRAFT) by

Contains all the functions and their usage of MySQLi procedural extension in PHP.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Opening a connection to MySQL

Syntax:
mysqli_connect(host,username,password,dbname,port,socket);

Returns an object representing the connection to the MySQL server.

Example:
$conn = mysqli_connect("localhost","my_user","my_password","my_db");

mysqli­_er­ror()

Return the last error description for the most recent function call, if any.

Syntax:
mysqli_error(connection);

Example;
echo("Error description: " . mysqli_error($conn));

Creating a Databasse

// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . mysqli_error($conn);
}

mysqli_close($conn);

Selecting Data

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);

mysqli­_nu­m_r­ows()

Return the number of rows in a result set

Syntax:
mysqli_num_rows(result);

Example:
 $rowcount=mysqli_num_rows($result);

mysqli­_fe­tch­_as­soc()

Fetch a result row as an associative array.
 

Closing a connection of MySQL

Syntax:
mysqli_close(connection);

Example:
mysqli_close($conn);

mysqli­_co­nne­ct_­error()

Return an error description from the last connection error, if any.

Syntax:
mysqli_connect_error();

Example:
die("Connection error: " . mysqli_connect_error());

Create a Table

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// sql to create table
$sql = "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
)";

if (mysqli_query($conn, $sql)) {
    echo "Table MyGuests created successfully";
} else {
    echo "Error creating table: " . mysqli_error($conn);
}

mysqli_close($conn);

Selecting a Database

Syntax:
mysqli_select_db(connection,dbname);

Example:
mysqli_select_db($conn,"test");

mysqli­_qu­ery()

Perform queries against the database.
For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object. For other successful queries it will return TRUE. FALSE on failure.

Syntax:
mysqli_query(connection,query,resultmode);

Examples:
mysqli_query($con,"SELECT * FROM Persons");

mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");

Inserting into Table

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);