Author Archive

PHP – problem triying to conect with Informix on IIS

I was working on a project which needs PHP talk to Informix database on Windows platform. After installed IBM Informix Client_SDK, included PHP extension php_ifx.dll, and use following command to connect to Informix:

$sync_link_id = ifx_connect($database, $username, $password);

I got following error:

[SQLSTATE=IX 000 SQLCODE=-25560]

If you check Informix document from IBM, you know Environment variable INFORMIXSERVER must be set. We know how to set up environment variable on Unix and Apache, but the question is how to set it up for IIS?

People may first think this could be environment variable on Windows as well. But this is wrong. Informix client is using Registry KEY on Windows platform. Here is the path on a 64bit machine.

HKEY_LOCAL_MACHINE\Software\Wow6432Node\Informix\Environment\

Second, you should have your default server setup under KEY:

HKEY_LOCAL_MACHINE\Software\Wow6432Node\Informix\SqlHosts\

At here, you need to provide HOST, PROTOCOL and SERVICE, so php_ifx knows how to connect to your Informix server.

After set them up, PHP should be able to talk to Informix.

Paging the query result

If you have 5000 products in database, you may like to show them in pages on the screen. It’s a common request to only retrieve the records on a certain page. This query must be quick and efficient if we are talking about millions of records in a table for an AJAX control.

In Oracle, we can do this:

SELECT * from
(
SELECT *, ROWNUM as rownumber from Product order by name
)
where rownumber between (PageNbr - 1)*PageSize + 1 and PageNbr*PageSize

As you can see, this query needs to fetch all rows first then narrows down the result. A more efficient query is:

select *
from ( select a.*, rownum rnum
from ( select * from product order by name) a
where rownum <= PageNbr*PageSize)
where rnum >= (PageNbr - 1)*PageSize + 1

Oracle will optimize this query, so it is not as resource intensive as the first one.

In SQL Server 2005, we can do similar query:

WITH Temp AS
(
SELECT row_number() OVER (ORDER BY [name]) AS rowNum, * from Product
)
SELECT * FROM Temp
WHERE rowNum between (@PageNbr - 1) * @PageSize + 1 and @PageNbr * @PageSize
ORDER BY [name]

The query had been optimized in SQL Server as well.

Google App Engine is open for Java Now

 Google App Engine Java Overview.

You can sign up here.

The Four Pillars of ASP.NET

Paul Litwin posted an interesting article that discusses the four pillars of ASP.NET (Web Forms, MVC, AJAX, and Dynamic Data).

He said: “Microsoft used to present ASP.NET Web Forms vs. MVC as a choice between a car and a motorcycle. Both will get you to your job , but some (the majority of the population, I might add) prefer driving a car, while a sizable minority love their motorcycles which give you better gas mileage and independence, but don’t protect you in the rain. To stretch this analogy to its breaking point, let me suggest that ASP.NET AJAX is like riding a bicycle to work (lean and mean, best gas mileage, but it requires you to exercise for your commute and exposes you to the elements like the motorcycle) while Dynamic Data is like taking the bus to work (let metro do the driving for you.)”

You can read his blog entry here:

VS2008 Tips & Tricks

As a .NET developer, I’m sure you have your own tricks to make your life easy. But you may still like to know more tricks on Visual Studio. Read this article to see if you know all of them…

Essential Visual Studio Tips & Tricks that Every Developer Should Know

Math Error in JavaScript

Believe it or not, if you do this in JavaScript: 103 x 67.12, you will get 6913.360000000001. This happens on both Microsoft Internet Explorer and Firefox. Try this your self.


Basic Math Errors in JavaScript

The calculation… Should equal… Your browser’s
  answer…
81.66 * 15 1224.9
103 * 67.12 6913.36
24.88 + 4.35 29.23

In terms of mathematical accuracy, 81.66 times 15 unequivocally equals 1224.9, as any sixth grader with a pencil and a piece of paper can demonstrate. But computer is in binary world. it has to convert decimal to binary number before calculation and there may be a problem to convert floating point decimal to binary. This is same for all computer languages. To avoid this, you need to know the scale (the number of decimal positions). Then you can convert the floating point decimal to an integer and scale it back at the end. i.e. (67.12 * 100) * 103 / 100. In Java, BigDecimal can be used to setup the scale.

JavaScript has a built-in method – toFixed, can be used to fix this issue easily as well.

amount = 103 * 67.12;
result = amount.toFixed(2);

So don’t forget this when you deal with a floating point decimal even for addition or multiplication.

The custom tool ‘MSLinqToSQLGenerator’ failed. Unspecified error

There is an issue in Microsoft’s latest SP1 for Visual Studio 2008. If you have a partial class with Using statement at the top of the file for a Linq-To-SQL Data Classes context object. You will get above error. Visual Studio will delete the corresponding “.designer.cs” file for your data context and nothing will be compiled.

 To solve this issue, you may have to move “Using” statements inside namespace declaration. This is a workaround provided by Microsoft.

A circular reference was detected while serializing an object of type …

I ran into an issue earlier when trying to serialize an object return by LINQ. After Googling it, I found following blog which is very helpful.

LINQ to SQL and Serialization

I believe this is a common issue for LINQ objects and hope this blog can save time for somebody.

Improve ASP.NET Performance and Scalability

Do you have performance issue on your ASP.NET website? Do you want to boost your site’s scalability? Do you know the bottleneck of your site? Do you want your site performs well under 100 times more traffic? Here are some good posts about this:

10 ASP.NET Performance and Scalability Secrets

Boost ASP.NET performance with deferred content loading

Using UTF-8 in Ajax, PHP, MySQL & ASP.NET

We have a project which needs to show foreign language, like Chinese, on a web page. And we are using RICO 2.0, PHP, MySQL & ASP.NET in the project. To achieve this, we decide to use UTF-8 character set and the following changes need to be done:

  • MySQL:

To store UTF-8 in a table field, you need to set column’s charset => utf8 and collate => utf8_unicode_ci

  • Web Page:

To show foreign language in a web page, the page file has to be saved as encoding UTF-8

  • PHP:

The default encoding for PHP 5 is UTF-8, so it supports foreign language. But if you use PHP to retrieve UTF-8 characters from MySQL, you must add mysql_query(‘SET NAMES utf8′); after connecting to MySQL database for setting up the connection encoding.

  • RICO:

As for RICO Live gird, Ajax response must start with <?xml version=”1.0″ encoding=”utf-8″?>

  • ASP.NET:

First make sure you have following line in you web.config
<globalization requestEncoding=”utf-8″ responseEncoding=”utf-8″ fileEncoding=”utf-8″/>
Second, keep both .aspx and code behind file save as the same encoding UTF-8
If you need to access the UTF-8 characters in MySQL database, be sure to use MySQL Connector/Net 5.0 or up with undocumented setting: charset=utf8 in connection string. We tried ODBC driver for MySQL, it doesn’t work properly with UTF-8.