Listing all the curl constants and viewing them in PHP – how to see the curl opt integer constants

Here is the thing :), I was working on some API where there is an example of cURL usage for it. For some weird reason the developer has decided to use the php cur option integer constant rather than the nice constant names [listed here].
So i have to use this little snippet to reverse match them..
No biggie, just sharing

$all_constants=get_defined_constants(true); // list all defined constants in the world
$curl_opts=array_flip($all_constants['curl]);
ksort($curl_opts);
print_r($curl_opts);

Changing column datatype in mysql

From time to time I have been changing the column datatype for different reasons. Majorly being for performance and efficiency and some other time based on how the mysql engines would be strong on some types vs the other
here is the simple command i use for changing the column datatype

alter table table_name modify column column_name datatypegoeshere
eg.
ALTER TABLE members MODIFY COLUMN date_created TIMESTAMP;

Adding user to sudoers in Mac

Working on mac, you might find something the sudo not working as expected or something.
Specially, if you are installing to ‘somehow’ restricted folders like /usr/local/bin ..
Here is what you can do to have an sudo access:
First check the group wheel (mostly) by running the following command

ls -la /private/etc/sudoers 

This shall produce something: -r–r—– 1 root wheel 1242 Jun 22 2009 /private/etc/sudoers
This will tell you the sudoers should be in the wheel group

Then check in what group the user currently is using

id 

From the listed groups, if wheel is not listed then add it

sudo dscl . -append /Groups/wheel GroupMembership 

-_-

Firefox adds xls extension when downloading excel files from problem

My problem is this.
I have this excel report file that I crafted using PHPExcel for my php application. I don’t have any problem accessing the file from my ubuntu machine with major browsers. But from my windows box, FF will add this .xls extension on top of my file name [myreport.xlsx becomes myreport.xlsx.xls] and this would confuse windows on how to create and will finally decide to open it as binary gibberish texts.
The solution is to assign the correct MIME type for the xlsx extension. I got that gem from here

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

phtml, php5 php3.. do not render on Apache webserver solution

Php has more extension than the customary ‘.php’.
If you are using for some reason the extension other than php, then you have to let Mr Apache that you intend to treat .phtml just like that of php files.
You can accomplish this using a simple .htaccess file that sits on the root folder of your website.

AddHandler application/x-httpd-php .php .php3 .php4 .php5 .phtml .html .htm

Yup that should take care of the mess.

Assembly code example – square a number

Here is another example of assembly code that would square a number:

#Simple square function in assembly language for x86 architecture
.code32 #tell the assembler we are using 32bit

.section .data #we don't have nothing in the data section
.section .text

.globl _start
_start:

    push $9 #fixed value of 9
    # The call updates the eip - instruction pointer
    call square 

    mov %eax, %ebx #the return value would be on register %eax, so pass it on %ebx
    mov $1, %eax #system call for exit on eax register - that would be 1
    int $0x80 #interupt

.type square, @function
square:
    mov 4(%esp), %eax #read the value to square basing from the stack pointer (esp)
    imul %eax, %eax # umm.. whatelse? multiply the value and store it on eax
    ret #return the value 

do the following from the command line to run the code

$ gcc -m32 -nostartfiles -o square square.s 
$ ./square 
$ echo $?

Shall produce 81 on the command prompt

Simple Hello world using x86 (32bit) assembly language using function

here is some hello world function that would just add 10 to whatever number it is given

#Simple adder function in assebmly - just adds 10 the called number 
.code32 #if running the code on 64 bit machine
.section .data
.section .text

.globl _start
_start: #entry point

    #Assign the number to be modified here
    push $9 # using immediate access mode just assign number 9
    call adder10 #call the function adder10 here - basically assign a memory address name

    mov %eax, %ebx #pass the return value to ebx since return value sits on eax
    mov $1, %eax  # call system call exit on register eax
    int $0x80 #yup - interrupt it!

.type adder10, @function # like declare a function here ;)
adder10:
    #save the current base pointer first
    push %ebp

    #load the current stack pointer to the base pointer and use the new base pointer in the function
    mov %esp, %ebp

    #load the paramter on register ecx
    mov 8(%ebp), %ecx #access it using base index access method
    add $10, %ecx #do the magic

    #the return value will be on %eax
    mov %ecx, %eax

    #return the original values to the ebp and esp
    mov %ebp, %esp
    pop %ebp
    ret

Save the above file as adder.s
to run this if you are on 64 bit machine use

gcc -m32 -nostartfiles -o adder adder.s

otherwise

as -o adder.o adder.s
ld -o adder adder.o

and run it as

./adder

to see the result

echo $?

If all ok the above should print 19

Segmenation fault – core dump error assembly 32bit code on 64bit machine

I have a shiny 64 bit dell machine that tops ubuntu on it.
I working working on some other machine a bit of assemly code and I didn’t have a problem.
But when I run the same code my 64 machine i was getting the a segmentation falut error. Not surprising actually.
here is what I have done to resolve it:

1. Don’t forget to add .code32 at the beginning of your assembly file. [ talking about x86 architecture ]
2. Have the gcc multilib on your machine

sudo apt-get install gcc-4.7-multilib

3. When assembling and linking your file use

gcc -m32 -nostartfiles -o executablename filename.s

Change your executablename and filename.s accordingly

A hello world in Assebly language tha would show the first number that is greater than 80

# 
# Simple program to stop when it gets the first number greater than 80
#

.section .data
number_store: #fixed name for the address holding
    .long 34,78,23,90,12,234,76,23,1 #Numbers to be dealt with. 
.section .text
.globl _start

_start:
    mov $0, %esi #initialize the esi 
    mov number_store(, %esi, 4), %eax # load the first number on the eax register

    begin_loop:
        cmp $80, %eax #compare the value on eax with 80
        jge exit_door #yup exit if the comparison met..
        inc %esi
        mov number_store(,%esi,4), %eax #load the next number from the array
        jmp begin_loop #go back to the loop

    exit_door:
        mov %eax, %ebx #load the last value we have to ebx register
        mov $1, %eax # call the exit call here
        int $0x80 #interubt it

This will be running on the x86 processor.
To run it on the linux do the following from the command line:
The following command would- create the object file:

as greater.asm -o greater.o 

The next one will be the linker – we need it to create the executable

ld -o greater greater.0

and run it as

./greater

Now the value would be accumulated on the register ebx.. Since the exit parameter will be stored on the ebx, and we have a value being loaded on the ebx it will be just showing that:

echo $?

this is A HELLO WORLD TO ASSEMBLY

Allowing others to use mysql from user machine on ubuntu- simple one!

It is customary sometimes to share the part of the database from sandbox to a fellow developer or to access it from the other machine – umm.. is it not how the servers are doing it?? what are you talking about 😉

Anyway here is a simple note to make it happen.1. We need to bind the machine name for the server. to do that
open your my.cnf – inside the /etc/mysql/my.cnf for debianish machines and search for the
[mysqld] and add the following
bind-address = your ip address goes here
you can find your ip address from

ifconfig

then save the file and restart your mysql

sudo /etc/init.d/mysql restart

There are ways to assign for a specified user from the specified ip addresses as well.
Like if you have database db1 and user1 from ip address 168.192.3.4 then

grant select, insert, update on db1.* to 'user1'@'168.192.3.4'