# Using `mysqli` to Connect to `TiDB`

+ **`mysqli`** class
  + **`mysqli(hostname, username, password)`**
  + Specify a database to connect to as well as `port/socket` name details, as `TiDB` commonly runs on port 4000 instead of the default `MySQL` port of 3306 this is likely to be required
  + The **`connect_errno`** and **`connect_error`** properties store the error number and error message of the last connection error
  + The **`query()`** function executes the SQL statement that is passed to it as a string parameter
+ **`mysqli_result`** class
  + A result set that is returned by executing **`$mysqli->query()`**
  + Includes various **`fetch_*()`** methods for retrieving data from the result set, for example: **`fetch_assoc()`**

---------------------------------------------------------------
The `connect_errno` property of the mysqli instance stores the failure condition of the last connection. If the connection is successful, connect_errno is equal to zero. Otherwise, it contains a number representing the error.

The `connect_error` property contains the error message of the last error (if any).

The `query()` method of the mysqli instance executes the query contained in its string parameter and returns a `mysqli_result` object containing the result set, or it returns false if there are no results or if there is an error. 

When executing statements that do not return a result set, such as `CREATE` and `UPDATE`, the `query()` method returns true on success.

The `fetch_assoc()` method of the `mysqli_result` instance returns an associative array for each row in the sequence, starting with the first row and moving to the next row each time the method is called. The associative array contains a key-value pair for each combination of column name and column value in that row. For example, `$row['first_name']` contains the value in the `first_name` column for the current row, as returned by the most recent `fetch_assoc()` call.

If the result set in the `mysqli_result` instance is empty, or if there are no rows left in the result set, `fetch_assoc()` returns NULL. This is useful in a `while` loop condition iterating over all rows in a result set, because NULL evaluates as `false`.

