PHP Database Queries

There are three main types of query you are likely to need to execute on your MySQL database from within your PHP scripts:

  • select – select and return rows from the database
  • insert – insert new rows in to a database table
  • update – update the information stored in a row
  • delete – remove a row from the database entirely

All of these queries can be executed using the mysqli_query() function. This function will return false if the query fails for some reason, and true if it succeeds.

PLEASE NOTE: mysqli functions differ from mysql (previous library) ones as in most of the cases they will ask the mysqli $link to the database connection. In our examples we will call it $dbc (DataBase Connection).

In the case of a select query, the mysqli_query() function will return a resource identifier which can then be used to access the data returned by the query.

In any case, a resource identifier evalutes to “true” in PHP so a true return value will always indicate a successful query. But be careful! Even if your query has been successful (returns true), it doesn't always mean you have done what you wanted.

To be sure you have effectively modified your database you can use mysqli_affected_rows(), while to check you have actually selected something you can use mysqli_num_rows().