When writing an application in PHP, I like to use Justin Vincents ezSQL for database access. ezSQL allows you to write your query and get your results as a PHP object, as shown in the following example:
// Select multiple records from the database and print them out..
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $users as $user )
{
// Access data using object syntax
echo $user->name;
echo $user->email;
}
So one common task you might want to do is to present the retrieved result as a table. While the ezSQL library features a debug() method, this should obviously only be used during development. So if you want to display your result as a table, you’ve got to do it yourself and I hereby provide a snippet of code on how to do it…
So after you’ve fetched your result from the database you can use the following method to print a HTML table with your results:
Read the rest of this entry
For an university assignment, I had to implement a recursive function to calculate the digit sum of a given number. I place my solution here for my own reference:
public static int digitSum(int n) {
if(n < 10)
return n;
else
return n % 10 + digitSum(n / 10);
}
This code can also be found in the GitHub repository that I am keeping for university: ChecksumTool.java
When comparing floating-point numbers (float, double) in Java, we quickly discover that we get roundoff errors. This has to do with the limited precision of Java floating point variables. The following code example shows the problem at hand:
double r = Math.sqrt(2);
double d = r * r - 2;
if (d == 0)
System.out.println("sqrt(2) squared minus 2 is 0");
else
System.out.println("sqrt(2) squared minus 2 is not 0 but " + d);
Theoretically, d
should be 0, but because we have limited precision (see the documentation on primitive data types) there will be a difference:
sqrt(2) squared minus 2 is not 0 but 4.440892098500626E-16
One possibility to circumvent this problem is to define a constant value (the following example uses EPSILON
). We then check if the difference is smaller than that constant value. Since we received a very small number (4.4E-16) above, we can use 1E-14 as the value of our constant:
Read the rest of this entry
For habere.ch, we launched a contest where we award a restaurant voucher to a random person that wrote a review of a restaurant on our site. For this, we will be using the following query:
SELECT DISTINCT comment_author,comment_author_email
FROM wp_comments
ORDER BY RAND()
LIMIT 1;
What this query does is, that it selects all distinct rows, orders them randomly and shows the first entry of this selection.
Note that this query is relatively slow and should not be used in a program. When you use EXPLAIN
on this query, MySQL will explain to you that it will create a temporary table and run a sort. Then only one entry is returned. But when you just use it once to determine a winner, this will work just fine!
Today I had to import some data from a CSV file into a table on a MySQL server.
So here is how to do it:
LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3);
Source
Of course, you will need to have access to the machine where the database is running. As an alternative, I am sure there are developer tools that can enter the data remotely.
Due to popular demand on my other post including the build process, I hereby provide the compiled version of the Tanuki Java Service Wrapper 3.4.1 for Windows x64 (Community Edition) for you to download. The software was built using the instructions included in my other post.
Use this package at your own risk, I provide no support or guarantee that this software works as advertised.
This software is provided “AS IS” and any expressed or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed.
To run the Service Wrapper, extract the following three files from the archive:
- bin/wrapper.exe
- lib/wrapper.dll
- lib/wrapper.jar
Download: wrapper_3.4.1_krenger_build.zip (aka wrapper-windows-x86-64-3.4.1.zip)
Newer versions: Newer versions of the wrapper are available here.
The only thing I ask in return is a comment if this actually helped you :)
To launch any Java program as a service, you might have to use a wrapper. This article shows how to build a current release of the Tanuki Service Wrapper (x64) from source. Tanuki Software provides three versions of the Java Service wrapper:
- Professional Edition (paid)
- Standard Edition (paid)
- Community Edition (free)
The paid versions of the wrapper require the configuration file to contain a machine-specific license key in order to function. When you use any x64 version of Windows as your host system, you are forced to either buy licenses or build the Wrapper from source, since they do not provide the binaries for the Community Edition. So here, we are going to build the Community Edition from source.
Update: I added a compiled version for Windows x64 in another post
Newer versions: Newer versions of the wrapper are available here.
Read the rest of this entry
Today I wondered how my newly purchased Virtual Server performs under load. I am currently using lighttpd as a webserver and MySQL as the database for WordPress. I already knew that MySQL features the Query Cache, which stores the result of issued queries in memory (Oracle’s equivalent of this would be the Result Cache in Oracle 11g). I wondered how much this feature could improve performance on a normal WordPress blog.
I quickly wrote a small Java program to query my website (source code available here: TestWebPerfo.java) and retrieve the performance metrics from the HTML source code. I then ran it 500 times to get my metrics without the Query Cache turned on. Average time for building the website: 0.115 seconds.
Read the rest of this entry