Montag, 13. September 2010

Some useful PHP functions

I spent most of the passing weekend coding on the msghub-project. In fact I only coded at night cause that seems to be the time when I can code best. I'm normally coding until 2-4 am and then sleeping until 2-4 pm :D

We've made some progress this weekend. You can now enter messages and they will be stored to the database. It's not a great deal, I know, but it's a first step.
@Pundidas was even able to write the code needed to send email!

I had already done some php in a course this summer. Back then I've written a very simple blogging-system. For the things I was doing this weekend I could use a lot of the code I've written back then and some of the experiences I made back then.
However there are many things I want to do better this time. So a lot of Google-research was part of my work this weekend.

I was looking for solutions for some problems involved with writing data to MySQL database and with processing data and strings.
Even thought it's nothing big I've discovered I wanted to share some of the things with you. Maybe someone will one day be happy to find this post (probably not, but at least I will be able to remember better :p).

I like the idea of tags and so I wanted to include them to msghub. So I've built a filed where you can enter comma-separated tags. I then needed a possibility to process each tag by it self. So first I had to split them up. For this task I used the function exploed. It will split a string on a specific symbol (in my case the comma (,)) and write the substrings into an array. Very useful!
Next I wanted to remove the spaces at the end and the start of the tags so there would not be the same tag twice in the database (once with and once without spaces). PHP offers a dedicated function for this task too. It's called trim.

After writing some code to process the tags I've written some that would read the tags from the database. I got some problems with that. I could not process the date I read from the database. For some reason it just didn't work. After about one hour(!) I found out what I was doing wrong.
The data was somehow 'wrong formated' or something. However the method mysql_fetch_assoc solved the problem :)

Two other methods useful for processing strings are strlen, which returns the length of a string and substr, which can be used to cut strings. It takes the start point, where the substring shall start and how long the substring shall be.

At the start of the post I promised you a method for database access.
Now the problem I had was that after writing some data to the database I had to find out which was the primary key of the entry I just generated. Of course I could just search for the highest entry, but when thousands of operations would be performed to the database that might cause errors and return wrong values.
So I was looking for something better. And again it turns out that PHP offers something for exactly this task. It's called mysql_insert_id(). But I'm using MySQL improved so it's mysqli_insert_id. They are both doing the same. They return the last PrimaryKey that was written to the database from the clien't connection over which the request comes. So you don't even have to be worried that someone else is writing an entry to the database before you can read out this value. GREAT!

Keine Kommentare:

Kommentar veröffentlichen