How to Insert Ignore or Get ID in MySQL

The MySQL "insert" statement adds a new record to your database. After you use this statement, you can either ignore the new ID added to the database or retrieve it in a new variable. You use the new ID to reference the new record in your MySQL code or in your Web pages. The way you set up the MySQL code determines if you ignore the new ID or get it for your applications.

Instructions

    • 1

      Open the Query Browser software in the Windows program menu on the database server. Log in to the server with your username and password when prompted.

    • 2

      Insert a new record to automatically create a new ID in the database system. If you use just this statement, the new ID is ignored by your code and just inserted into the table. The following code creates a statement that inserts a new customer into the table:

      insert into customer (name) values ('Joe Smith');

    • 3

      Use the following code to retrieve the ID for the new record:

      newid = mysql_insert_id(&mysql);

    • 4

      Use the following code to print the new ID to the screen to debug and test the MySQL code:

      print newid;

Related Searches:

References

Comments

Related Ads

Featured