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 21 2010

Learning how to learn

One of the most important lessons that I have “learned” is the invaluable asset of knowing how to learn. This may seem like a simplistic if not empirical skill that everyone surely possesses by simply finishing any flavor of structured education, but I would actually argue that this seemingly simple skill is often overlooked and undervalued. In my opinion, anyone willing to take note of how they absorb information best will be much more successful at mastering a subject/skill.
Learning most commonly occurs through visual, auditory, interactive means and the majority of people are better at one or another when trying to retain information. The first discovery should be to figure out through which channel you are best at retaining information: eyes, ears, or hands. I am very much a visual learner. I think visually and do math visually and consequently learn better through visual means. I also learn a great deal by doing something. So, before you hone your learning skills, identify your strengths.

Secondly, I believe that knowledge is power, power is valuable, and anything valuable doesn’t come without sacrifice and hard work. The basis of this whole entry is to reveal the following story about how I was forced to learn how to learn by a few students in my high school.

When I was a freshman in high school (attending Donelson Christian Academy), I was unfortunately in a serious car accident which left me with 3 days in intensive care, a severe concussion, lots of stitches from broken glass, and a broken ankle. The injuries prevented me from participating in sports that Fall. In place of the after school practices, I decided to enroll in an elective course dealing with the Internet (this was 1996 so we were pretty hip!). As you might expect, the guys in this class were those audio/visual, computer geek-types that everyone steered clear of. They even had pocket protectors (no joke!). I was a bit hesitant at first but soon realized they were really nice people with very valuable information. So, as I began to watch them program CGI perl apps, buy domain names, and install Linux from floppy disks, I wanted in. As I soon discovered, I enjoyed technology as much as them.

In order to get involved, I began asking questions and asking for guidance. This approach however was not looked upon kindly by the guys who had put in the hard work and time to learn these skills themselves. They wanted me to earn it the same as they had so they started handing me books to read (mostly technical manuals or programming specs). I dug in. They wanted me to learn how to teach myself by reading and researching. This turned out to be one of the most valuable lessons of my life. Instead of relying on someone telling me the answers to my possibly ignorant questions, my first stop was to consult the documentation. With the proliferation of Internet resources and the power of the Google search, there is no excuse for anyone trying to find information when they want it. The information is endless. Being willing to explore and knowing how to learn are all you need to participate.

Get involved!


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