PHP: eval() without output to screen

Sometimes, we need to eval() some lines of code and grab the result without output to screen. As you know, eval() has only one parameter, so we have to use output buffer for that purpose. The following code can achieve that. Pretty clean and understandable: ob_start(); eval(‘echo “Hello world!”;’); $str = ob_get_contents(); ob_end_clean(); $str will hold the output returned by eval function.

Format post date in WordPress

We all know that $post->post_date will display post date in the following format: 2011-08-19 17:23:25. The problem is that, if we want to display post date in different format, what should we do? Fortunately, WordPress has a very useful function mysql2date. Suppose you want to display only Month and Year of the post, you should be able to do that using this snippet: < ?php echo mysql2date('F Y', $post->post_date) ?> The first parameter of this function can be referred in date function of

How to count number of search results returned by WordPress

Suppose you want to create a custom search result for your theme. Apparently you will create a php script named search.php. One problem is that, you want to display total number of search results returned. Here is the solution for that. Searching for: < ?php echo get_search_query(); ?> < ?php $allsearch = &new WP_Query("s=" . get_search_query() . "&showposts=-1"); $count = $allsearch->post_count; ?> < ?=$count?> results found: Happy coding!

Host … is not allowed to connect to this MySQL server

Suppose you’ve installed LAMP (Linux, Apache, MySQL, PHP) in your server. Everything is working fine. One day, there was a problem on your server. You don’t want to use the command line mode of MySQL because it is very difficult to debug. So, you grab a tool, for example SQLyog to connect to that server. Surprisingly, you cannot connect and the sever give you a weird message: ERROR 2003: Can’t connect to MySQL server on ‘host_name’ (111) This is a very common problem. You forgot to change the bind address on my.cnf. Use Vim, Nano or any text editor to open /etc/mysql/my.cnf as root and change the bind address from (default) to Now your MySQL server are open to the world (be careful!) You might think now you will be able to connect. Things are not easy like that. You will get another error message: Host … is not allowed to connect to this MySQL server If you have nmap installed, you can see your server is already open port 3306 (which is MySQL default port). So, why you cannot connect? MySQL user table has something like ‘user’@’host’. By default, there is only 2 entries: ‘root’@’localhost’ and ‘root’@’’. You have to add one more entry. Connect to MySQL server and type the following: GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘root-password’ WITH GRANT OPTION; Notice that ‘%’ is wildcard for “any host”. You will be able to login now. Good luck!

Why are private fields not private to the instance?

In C# it’s perfectly legitimate to access private fields of other instances of the same type. C# specification also state that access to private fields is on a type, not an instance. Is there any reason for that? public class Foo { private bool aBool; public void DoBar(Foo anotherFoo) { if(anotherFoo.aBool) … } } One reason for this is that, access modifiers work at compile time. As such, determining whether or not a given object is also the current object isn’t easy to do. Another reason for this is that, the purpose of encapsulation is to lower mutual dependence of different pieces of code (classes in C# and Java), not different objects in memory. For example, if you write code in one class that uses some fields in another class, then these classes are very tightly coupled. However, if you are dealing with code in which you have two objects of the same class, then there is no extra dependency. A class always depends on itself. Source: