Thursday, February 12, 2009

TSQL & Conditional If

0 comments
Have you ever needed to do a quick if in a single line assign a value and get out without having to break out the typical;

begin
declare @val int;
declare @text varchar(25);

set @val = 2;

If(@val = 1)
set @text = 'yes';
else
set @text = 'no dice';

print @text;
end

Me also.

With a little thought in it I realized this is a simple on liner;

begin
declare @val int;
declare @text varchar(25);

set @val = 1;

select @text = case when @val = 1 then 'Yes' else 'No Dice' end;

print @text;
end


Now here is the clincher. You can actually test further is you need to check for other conditions.

begin
declare @val int;
declare @text varchar(25);

set @val = 10;

select @text = case when @val = 1 then 'Yes'
when @val = 10 then 'cool 10'
else 'No Dice' end;
print @text;
end


Hope this helps a bit, now back to coding...

Monday, February 2, 2009

Debugging PHP using Netbeans

0 comments
First off you will need to download the DLL for debugging;

php_xdebug-2.0.3-5.2.5

After you download the dll, rename it to php_xdebug.dll

Then place this file in your php/ext directory.

After you place the file in the php/ext directory, back up your PHP.INI file before editing it.

Open your PHP.ini file.

go to the bottom of the file and add the following;
 [xdebug]
zend_extension=xdebug.so
; example c:\php\ext\php_xdebug.dll
zend_extension_ts="your_drive:\path_to_php\ext\php_xdebug.dll"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.idekey=default
xdebug.extended_info = 1
xdebug.remote_autostart=1
; for remote connection
xdebug.remote_port=9000
xdebug.remote_host=localhost
;xdebug.show_local_vars=on

Save the file and restart Apache.

FYI: When you are not debugging you may want to comment out the lines you added above.

Sunday, December 28, 2008

PHP XDebug on Windows Remoetly with NetBeans

0 comments
Trying to get XDebug to work remotely was a little interesting to say the least.

After some searching I was able to get it up and running, but I can tell you the answer was not in one place unfortunately.

If you have PHP running on a windows machine and want to connect to it using NetBeans 6.5 remotely you will need to do the following.

Well you don’t need to, but if you want to debug remotely it might not be a bad idea :)

The first thing you will need to do is download the Xdebug extension for php and select the windows module to match the version of PHP you are running.

Next rename the dll you just downloaded to php_xdebug.dll and copy it to the drive:\path\php5\ext folder. After you copy the file you are going to have to edit the php.ini file.

You will need to append the following to the file:
  zend_extension_ts="drive:\path\php5\ext\php_xdebug.dll"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.idekey=default
xdebug.remote_autostart=1


Save the file and restart apache. Next make sure that Xdebug is enabled by running:

  phpinfo();


Now here is the problem I ran into. From the client where I was running NetBeans, I keep getting a timeout as it kept saying Xdebug was not configured correctly.

So after a little research I did the following.

I added:
  ; for remote connection
xdebug.remote_port=9000
xdebug.remote_host=[the ip address of the client connecting to this server ]


I also uncommented
  extension=php_sockets.dll


Restarted apache one more time and viola! It worked like a charm.

Hope this helps...

Friday, December 19, 2008

Getting the Computer Name from C# the unsafe way

0 comments
These I was on a snowy night coding away and I needed to get the name of the computer as I needed it for a ...

well that is another story for another time :) lets get onto some more important things like code, oh yeah!

I know that in C# all one has to do is call upon:

  string computerName = Environment.MachineName;
Console.WriteLine(computerName);


And there it is, all nice and pretty just waiting for you use.

The problem with this is that it is just way too boring and to be honest I am getting a little bored writing code in C# these days as it seems it just wants to do everything for you and personally I miss the C++ days (Java, C++ you guys listening, I am looking at you once again), so I figured I would go ahead and pull up my sleeves and have just a little fun even if only for a few mere minutes.

First I added the wonderful little include, wait I mean using ;)


using System.Runtime.InteropServices;

Then in the little util class I have going on, I added this little gem of an import.

public class Utility
{
[ DllImport( "kernel32.dll" ) ]
private static extern unsafe bool GetComputerNameW( char* name, ref ulong size );

...

}

Ah refreshing, takes me back a few years. Beautiful isn’t it?

Next I wrapped this beauty around a little function called getComputerName

public static string getComputerName()
{
string computerName = String.Empty;

// Dont forget to add unsafe.
unsafe
{
ulong size = 256; // Set size for allocation.
char* name = stackalloc char[ (int)size ]; // Allocate memory.

bool success = GetComputerNameW( name, ref size ); // Get computername.

for ( uint i = 0; i < size; i++, name++ ) // Assign to the string.
computerName += *name;
}

return computerName;
}

Function Complete! Would you look at that!

I better step away from the screen for a few minutes as I wipe away the tears…

Ok, back.

Now to use it, well, you will just call it:


string comp = Utility.getComputerName();
Console.WriteLine(comp);


Now come on, be honest, wasn’t that so much more fun than calling Environment.MachineName?

I say yes, but then again, that is just little OLD me, OLD keyword !

KDWAG out until next time…

Happy coding and Merry Christmas! Ok, not Christmas yet but soon I tell you soon!