Archive for June, 2007

func_get_args() No more function var definitions!

Saturday, June 30th, 2007

Well, I don’t know about you - but sometimes I get a bit frustrated with people attempting to send too much, or too little information to my class functions, so I began to implement variable free function calls. It gives a little bit of flexbility in how you can use it (which I won’t go too much into) and might not suit all users requirements. However, the simple call you want to run is this:

function Myfunction() {
$Arguments = func_get_args();
}

This will give you an array (if there are arguments) containing all of the variables sent to the function. The best way to deal with the arguments, is to do what you would normally do and assume that your function is setup like: function($Var1,$Var2) etc. Why would this be helpful then? Well, it allows you to write functions like this:

function MyFunction() {

$Arguments = func_get_args();

foreach($Arguments as $Argument) {
$Result[] = mysql_query($Argument);

}

}

Just a little handy tip.

mySQL Dump File Import

Saturday, June 30th, 2007

Ever wanted to quickly deploy database adjustments either remotely, or via some kind of bash script? It’s easy! Once you’ve generated the SQL file all you need to do, is at console punch in:

mysql -u [user] -p [pass] -h [hostname] [databasename] < [sql.file]

An example:

mysql -u updater -p spindle -h localhost sitedb < updatesJUN.sql

It’s as easy as that. Just remember, that it won’t give you a response, unless the process fails - and ALWAYS check your SQL is compatible and that your not going to bring the production system down by making your changes! Enjoy!

Annoying PHP Arrays

Friday, June 29th, 2007

Just a quick fix for a simple and higly common problem.

When you do a while loop on a function, pulling the responses into an array, such as:

while($Variable[] = mysql_fetch_array($Pointer));

PHP adds another empty array element to the end of your $Variable array as it doesn’t care that the response was FALSE, or NULL, it was a response so it pushes it into the array, then stops (as per your while query).

I have seen alot of fixes for this, some of which are highly complex:

$Count = 0;
while($Count<@count($Variable)-1 && !@end($Variable) {
@array_pop($Variable);
$Count++;
}

Which counts and such to keep track to make sure it doesn’t accidently array_pop forever! But really all you need to do (and the most simple solution by far) is to do 1 check, to see if the array is empty on the end and pop it:

if(!@end($Variable)) {
array_pop($Variable);
}

Or, if you want to cycle through the end of the array (such as, when you explode a URI with lots of trailing /’s) you can do this, a very simple version of the first one:

while(@count($Variable)>0 && !@end($Variable)) {
@array_pop($Variable);
}

Yes, simple yet effective solutions.

Subnet Masks - Quick Reference!

Friday, June 29th, 2007

I don’t know about you, but as a programmer I don’t care much for how the network topology or breakdown goes. As long as I can connect out and connect in I’m happy. Normally, when I am blessed enough to have to install a server into the data centre, I compile and build the thing at the office and am supplied by the network provider of our IP ranges, etc.

Unfortunately, in the modern day world of sub-leasing rackspace and what not and the misunderstanding that as a programmer, I must somehow have an exceptional understanding of the inner workings of network topology, I have often been supplied with nothing but:

Your IP is : XX.XXX.XXX.XX… It’s a /26

As helpful as that is (and as much information as is on the internet. It still bugs me. So here is the cheat sheet that I have found - I hope you find it as useful at decrypting network-speak, as I have.

 	Hosts	Netmask		Amount of a Class C
/30	4	255.255.255.252	1/64
/29	8	255.255.255.248	1/32
/28	16	255.255.255.240	1/16
/27	32	255.255.255.224	1/8
/26	64	255.255.255.192	1/4
/24	256	255.255.255.0	1
/23	512	255.255.254.0	2
/22	1024	255.255.252.0	4
/21	2048	255.255.248.0	8
/20	4096	255.255.240.0	16
/19	8192	255.255.224.0	32
/18	16384	255.255.192.0	64
/17	32768	255.255.128.0	128
/16	65536	255.255.0.0	256

Many thanks to: http://krow.net/dict/subnet.html

Soap Endpoints and Multiple Environments

Friday, June 29th, 2007

Tired of updating soap endpoints in your WSDL files when you move environments? Well if you use Ant/Eclipse - help is at hand.

With this nifty little Ant target you can replace all your endpoints in one foul swoop!

<!– Hack the WSDL to change the Endpoint to the Host Defined in Here! –>
<target name=”wsdlhack” depends=”init”>
<echo>Hacking WSDLs - Hack Hack Hack - Replace localhost with ${Server}</echo>

<fail unless=”Server” message=”Failed to Specify Server Name to rewrite WSDL”/>
<fail unless=”ServerPath” message=”Failed to Specify Server Path to rewrite WSDL”/>

<replace dir=”${ServerPath}” value=”${Server}”>
<include name=”**/*.wsdl”/>
<replacetoken>localhost</replacetoken>
</replace>
</target>

Just call the Ant Target specifying the ServerPath and Name and bingo! No more endpoint madness!

inmeta: bug in Google Mini

Friday, June 29th, 2007

There are a few features that lack some of the finer attention that they deserve in the GSA and the mini, but a bug that I snagged more recently was the way that the mini handles inmeta: requests. For some reason, if you push through an inmeta request with a space, the mini doesn’t parse this out propertly.

The quickest fix is to handle the replacement of the space before you send your request to the Mini, but of course, this will only work if you are pushing data into the mini without using the XSLT. Simply replace the spaces and other characters with the correct % mapping and you shouldn’t have an issue.

I’ve always found the best way to communicate with the Mini is to push data in as a direct request and to receive data back as XML. This overcomes alot of the ‘undocumented features’ of the mini. The Google Mini XML format is very easy to work with and exceptionally informational to the end application.

If you have a fix for the mini XSLT version of this bug - feel free to let me know.

Nulls in MSSQL Via FreeTDS

Friday, June 29th, 2007

Ok, this is another gotcha. If you are running Stored procedures on MSSQL via FreeTDS and the procedure has inserts into fields not explicitly set to allow nulls it will fail - however via query analyser it will work ok.

Problem is Query Analyser by default sets “ANSI_NULL_DFLT_ON ON” so the following code will work

Create table #Included_Usage (id int ,TotalUsage float null, EligibleUsage float null, EligibleLength int null, Airtime int null)
insert into #Included_Usage( NULL,’101′,’2′,2,4 );

However from FreeTDS it will bork. You have two options - attempt to set ANSI_NULL_DFLT_ON before running the proc, or change the proc to explicity allow nulls

Create table #Included_Usage (id int null,TotalUsage float null, EligibleUsage float null, EligibleLength int null, Airtime int null)

cI prefer the second alternative. I always like to err on the side of explicit definition, rather than implicit. Usually saves someone from bashing their head on something.

Debugging MSSQL Stored Procedure Problems over TDS

Friday, June 29th, 2007

Found this little gem (or rather tripped over it) recently.

We were calling some MSSQL stored procedures from an OSX Server with PHP5 / FreeTDS (0.6.4) and experiencing bizarre results. The stored procedure would simply halt midway through operations. Running the same query via Query Analyser and it worked flawlessly.

FreeTDS was the first suspect - having tripped over TDS Client configuration previously with MSSQL stored procedures doing inserts with NULLs. Butwe needed more information.

Running a trace via MSSQL Server yielded better results. We could see where the procedure was dying. Root cause - one of the MSSQL DBAs had a nasty habit of adding semi-colons after each declare statement. When calling from ODBC etc this didnt seem to be an issue - but FreeTDS saw the semi-colon - assumed the statement was over - and exited, stage left.

Lesson - semi-colons in MSSQL stored procedures are not necessary and best avoided like the plague if you wish to access the procedure via FreeTDS.

Updates - July

Thursday, June 28th, 2007

So we figured that there are too many people hanging around the WHS office, bragging about how good they are at stuff so we laid down the challenge. Put up, or shut up.

This is the result of that challenge.

WHS and MGSK have teamed up to provide the results to you all - the general internet populus. We hope that you find the information contained in here rewarding, informational, usefull and etc. If not, please feel free to let the author of the guide or trick, or solution know.

We havent got the site open to registrations as yet, but we will shortly. So stay tuned.

In the mean time, feel free to read around and tell us what you think via email:

helped@wehostsheep.net