Enabling PHP in Mac Mountain Lion

I have been using snow leopard and recently I got a new mac with mountain Lion. As a PHPer the first thing I was doing was to install php+mysql and I have found it much easier

here is how to enable apache for PHP:

1. go to terminal, click the launch icon from dock and you will find it in the utilities, and do
apachectrl start – you need a sudo access for this
2. Go to system preferences -> internet and wireless -> sharing and check web sharing
3. Now, go to http://localhost and verify the it works is there.
4. Then being on your editor, go to /etc/apache2 and open httpd.conf file. Then look for php5_module and uncomment that line.
5. You are done. For testing, go to /Library/Webserver/Documents/ and add a php file named phpinfo.php with content of phpinfo(); and access it from http://localhost/phpinfo.php

Happy PHP

Unicode error using PDO. Mysql Unicode problem

Are you having those ???? things on your webpage when something you entered on Mr mysql is unicode character?
Here is a fix for that

Assuming you are using PDO object in your Db Class..

$db = array containing information ....
$this->pdo = new PDO("mysql:host={$db['host']};dbname={$db['database']};charset=utf8", $db['username'], $db['password']);

Now the next task to to tell Mysql to honor unicodes:

$this->query('SET NAMES utf8');
$this->query('SET CHARACTER SET utf8');
$this->query('SET COLLATION_CONNECTION="utf8_general_ci"');

where your query function could look like

public function query($query, $params=array())
	{
		$results=array();
		if (!empty($this->pdo))
		{
			try
			{
				$stmt=$this->pdo->prepare($query);
				$stmt->execute($params);
				$results=$stmt->fetchAll(PDO::FETCH_ASSOC);
			}
			catch(PDOException $exception)
			{
				echo "error occured";
				//logit or whatever
				die();
			}
		}
		return $results;
	}

Yea this would take care of the problem. There are certain things you have to do on the mysql database itself to handle unicode but I will not discuss those here
Hope it would help somebody..

Convert list tag li to horizontal links using CSS

Anybody involved on the front part of the web would be using it since s/he is 3 or something..
but if your programming time logs heavily show you are working much on the backend stuff.. this might help a bit..

here is the given list
<pre>
<ul>
<li>link1</li>
<li>link2</li>
</li>http://gullele.wordpress.com</li>
</ul>
</pre>

do the following css and it would take of making it horizontal

<pre>

ul{

margin:0;

padding:0;

}

li{

display: inline;

padding: 0 0 0 0 – give appropriate values here based on your chice

}

</pre>

This shall produce a li tag being on the horizontal..

 

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