How to get the ID generated by the last INSERT

Many times there is a need to get the autogenerated ID from the last MySQL INSERT performed.

This ID can then be used to perform some more INSERTs particularly in different tables.

As a typical example lets assume you have members table and a seperate table to store their preference for receiving newsletter.
When a new member registers, we perform the first INSERT to add the member details in the member table.
Now our newsletter table needs the memberID generated from this insert so that we can store their preference (In case you are using their memberID as the primary key instead of the username)

They way to perform this is as below

Export data to Excel

Just write your own query within the function and call the function from anywhere in your script

 

Create a List of files within a certain directory

This function will list files and directories within a specified directory

E.g. if a directory named web contains some files like index.htm, contactus.htm and some folders like images, documents, products
The script will return

  • index.htm
  • contactus.htm
  • images
  • documents
  • products

$sortflag can be any of below
SORT_REGULAR – compare items normally (don’t change types)
SORT_NUMERIC – compare items numerically
SORT_STRING – compare items as strings
SORT_LOCALE_STRING – compare items as strings, based on the current locale.

PHP – MySQL Search on multiple columns using concat

More often there is a need to search part of a text in multiple columns and the search is based on a single keyword text box

For e.g. in a users table first name and last name can be stored in seperate columns

Let’s assume a user named Joe Smith stored in a users table with

first _name => Joe

last_name => Smith

If we have a single keyword text box search and if someone types “Joe Smith” and performs a search, a simple search query as below would give no results

For the query to give correct results it needs to be modifed as below