Mar 31 2010

Monster-sized scam!

Apparently, when i paid $125 for my HDMI cable for my TV, I got ripped. FYI if you are buying these types of cables anytime soon.

Read this


Budget Planner – Mint.com


Mar 30 2010

Twitter- Can you hear me?

This post is to test whether my post goes to twitter or not


Jan 29 2010

Moving/Backing-Up Innodb tables from Ubuntu to MacOSX or Windows *argh*

I just had the most frustrating and long battle with moving a MySQL database (both MyISAM and InnoDB tables) from my Ubuntu Dapper Drake 6.06 LTS. Currently, I have a MySQL slave server set up to accept a trickle down of data from the master of a VPN line to the office. This allows me to shutdown the slave without interrupting the production environment and take snapshots of the database. Well, I was doing my due diligence and wanted to check my restore process and write a shell script or little C program to handle some automation. Come to find out, when I restored my copy of the database on my Mac OSX (10.4.10) I would get an error saying that my Innodb tables didn’t exist (“ERROR 1146 (42S02): Table ‘db.table’ doesn’t exist”) but I was very careful when creating a binary backup of the files. I checked and double checked the config files and my Innodb set-up. Everything seemed in order.

After searching for days ( here and there, when I had the time to get caught in a rabbit chase ), I finally found a flag that I thought might show some effect. Indeed it did.

[mysqld]
lower_case_table_names=1

See: http://dev.mysql.com/doc/refman/5.0/en/moving.html

This however encourages this flag to be set before any tables have been created. Too late! So, I switched the flag to 0 and it fixed all my problems on both Mac OSX and Windows XP. Apparently, by default, this flag is set to 1 now. Hope this helps someone that is perplexed by this problem.

[mysqld]
lower_case_table_names=0


Jan 15 2010

HOW TO: Write an abstract database class in PHP5 (with MySQL)

So, if you are new to PHP or perhaps just new to creating web applications that connect to a relational database (such as MySQL), the following design pattern could help you get started on the right foot.

I am gonna guess that if you have started to create a web application/CMS that is composed of multiple PHP files (ie, news.php, calendar.php, pictures.php, etc), you are feeling the pain from all the database code that seems to be repeated. This is precisely the problem that I would like to help you solve. With an abstract database class, you can create generalized functions that will be flexible for later growth and unexpected twists.

class db {
/* Members */
private $db_user;
private $db_pass;
private $db_name;
private $db_server;
private $link;
private $result_id;

/* Methods */
public function __construct($user=DB_USER, $pass=DB_PASS, $name=DB_NAME, $server=DB_SERVER){

$this->db_user = $user;
$this->db_pass = $pass;
$this->db_name = $name;
$this->db_server = $server;

$this->connect();

}

private function connect(){

$this->link = @mysql_connect($this->db_server,$this->db_user,$this->db_pass) or die("can't connect to database");
@mysql_select_db($this->db_name,$this->link) or die("can't select db (".$this->db_name.")";

} private function disconnect(){

@mysql_close($this->link);

}
public function __destruct(){

$this->disconnect();

}

This will give us a good base to work from. We have established the constructor, destructor and the methods responsible for making and breaking the connection to the database. The reason we created a new function called connect and simply just wrapping the mysql_connect() function is because we want to the class to flexible enough such that if we wanted to change the database from MySQL to Postgres we could simply replace the mysql_connect() function with the pg_Connect() function without modifying potentially hundreds or thousands of database calls. (same would be true for disconnect())

Now that we can connect, we should create some functions that will allow us to SELECT something from the database.

public function select($params){

extract($params);

$sql = 'SELECT '.$fields.' FROM '.$table;

if(!empty($where)){
$sql .= ' WHERE '.$where;
}

if(!empty($group_by)){
$sql .= ' GROUP BY '.$group_by;
}

if(!empty($order_by)){
$sql .= ' ORDER BY '.$order_by;
}

if(!empty($limit)){
$sql .= ' LIMIT '.$limit;
}

$rows = array();
$this->query($sql);
return $rows;
}
private function query($sql){
$rows = array();
$this->result_id = @mysql_query($sql);

if($this->result_id){
$rows = $this->fetch_rows();
}
return $rows;
}

private function fetch_rows(){

$rows = array();

if($this->result_id){
while($row = @mysql_fetch_object($this->result_id)){
$rows[] = $row;
}
}
return $rows;

}

As you can see we have now created functions that will create an abstraction layer on top of the MySQL functions so that we can easily replace the “guts” of those functions (if necessary) and all the lines of code that reference those functions will not have to change.

Hopefully you get the gist of how the rest would go. I have included a more fleshed out version of the class at this link (db.class.php).


Jan 1 2010

Intro

Welcome to the newly formed saine.blog. My aim for this weblog is to recount the many important lessons I have been lucky enough to learn while striving to better my efforts to be a successful web application developer and Internet entrepreneur.

Some of the topics that I have pinpointed as my first posts are below:

  • Learning how to learn
  • Attention to detail
  • Story: How my competitor sold 49% for $18M
  • Partnering with friends
  • Story: The 1700% pay increase that changed my life
  • Raising money