Website Design Westchester | Graphic Design New York | Online Marketing Westchester | SEO NY | Corporate Branding Westchester New York | Application Development NYC


Getting Last Inserted ID Record in Coldfusion

February 6th, 2010

Problem

After inserting a record into a database the application needs the id of the record that was just inserted.

Solution

ColdFusion provides wealth of information in the result variable of a <cfquery> tag.

Detailed explanation

Often times an application needs to have the id of a record just interted into a database.  ColdFusion provides an easy way to get this id as well as a host of other information by specifying a variable name in the result attribute of the <cfquery> tag.  The variable that is returned is a structure. The result attributes is available in ColdFusion 7, however the information returned about the id is only available in ColdFusion 8 and later.

The following <cfquery> code specifies the result attribute:

<cfquery datasource="#dsn#" result="stResult">
INSERT INTO
tbl_Person
(
PersonName,
PersonEmail
)

VALUES
(
<cfqueryparam value="#PersonName#" cfsqltype="cf_sql_varchar"
/>,
<cfqueryparam value="#PersonEmail#" cfsqltype="cf_sql_varchar"
/>
)
</cfquery>

After the query executes the stResult variable will hold details about the query.   The general information keys are sql, recordcount, cached, sqlparameters, columnList and ExecutionTime.  The are available in ColdFusion 7.  With ColdFusion 8 additional fields were added to return the id of the last inserted record.  Depending on what database the application is utilizing the database specific field will be populated.

IDENTITYCOL – SQL Server

ROWID – Oracle (This is the ROWID not the primary key. )

SYB_IDENTITY - Sybase

SERIAL_COL – Informix

GENERATED_KEY – MySQL

The following code will output the ID of the last inserted record assuming the application is using SQL Server.

<cfoutput>
#stResult['IDENTITYCOL']#
</cfoutput>

A very simple and quick way to get the id of the last inserted record provided by ColdFusion

source: Coldfusion Cookbook

StumbleUponLinkedInShare

How to alternate row colors in PHP

February 6th, 2010

I’m not sure if there is an easier way to do this in PHP, but this is the easiest way that I found to alternate the color of rows in a table. First you want to set a variable to 0, I used the variable i in this example, but you can really use anything. Then you want to go and check if the variable can be divided by two. If it can then it’s even and you set the color to any color you want. Obviously if it cannot be divided by two then it is even and you set an else statement to set those another color. This will therefore set the even rows one color and the odd rows another. At the end of the while loop you want to add one to your variable (example: i+1).

See below for an example.

  1. <?php
  2. $i=0;
  3.  while($row = mysql_fetch_array($result))
  4.  {
  5.        if ($i%2)
  6.        {
  7.            $bg='999999';
  8.        }
  9.        else
  10.        {
  11.            $bg='666666';
  12.        }
  13.        $i=$i+1;
  14. }
  15. ?>

That’s basically it. My next article will be about how to display PHP code. I found it to be a little more difficult to do this so I think it will be good to

StumbleUponLinkedInShare
© Black Rhino Solutions, Inc 2009-2012 All Rights Reserved | Terms & Conditions | Privacy Policy
Web Design Westchester

* Means Mandatory

Full Name * Project Budget

Services Needed:






Your Role/Title Project Timeline
Phone Number * When do we start?
Email Address * Project Description *
Company Name
Website URL

To check that your human please copy the image on the left
website design westchester