2011/10/27

HTML entities in Textarea tag

Maybe this is obvious, but I just leaned it. See the example below,
<textarea>
techrecorder&reg;
</textarea>
I expected to see the '&reg;' but it gives me '®' So when the value of textara tag is presented by convering all of html entities code to the code result, for exmaple, '&lt;' to '<' and '&quot;' to '"'. So if you wanted to show html entities code in textarea, you have to escape &, for example,
<textarea>
techrecorder&amp;reg;
</textarea>

selectall_arrayref vs. selectall_hashref

Both selectall_arrayref and selectall_hashref are method that combines "prepare", "execute" and "fetchall_arrayref" into a single call. The former returns a reference to an array containing a reference to an array (or hash, see below) for each row of data fetched. And the latter returns a reference to a hash containing one entry, at most, for each row, as returned by fetchall_hashref(). Note that selectall_hashref return the ordered list of result as statement intented to, for example,
$sql = \"select ename from employee order by ename\"; 
$hash_ref = $dbh->selectall_hashref($statement, \"ename\");
The %$hash_ref does not contant the ordered list of ename, because it's hash. So the get the ordered list, you need to sort the hash. like
sort(%$hash_ref);
In some case, the statement has "order by" multiple fields, it's not easy to sort hash to having the same order as statement trying to, so you may just use selectall_arrayref, which can also return a reference to a hash, for example,
$sql = \"select ename, eage from employee order by ename, eage\";
my $emps = $dbh->selectall_arrayref(
      $sql,
      { Slice => {} }
  );
  foreach my $emp ( @$emps ) {
      print \"Employee: $emp->{ename}\n\";
  }
For more perl dbi usage, click here http://search.cpan.org/~timb/DBI/DBI.pm

2011/10/13

Restore Slave When it broke

When something goes wrong, slave throws error, one symptom will be that a lot of mysqld-relay-bin.xxxx files are accumulated in /var/lib/mysql/. So you try to restart slave, but got error,
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log
And you check mysql error log (usually at /var/log/mysqld.log), it says "111014 3:30:47 [ERROR] Error reading slave log configuration". How to do? The simple solution is that,
  • Stop slave, by "slave stop;" at mysql command line.
  • Remove all mysqld-relay-bin.xxxx.
  • Remove master.info and relay-log.info
  • Then start slave by "slave start"
That should work, but you may lost some positions. And if you wanted to start slave at current replcate log file, then you can run one more command before starting slave.
CHANGE MASTER TO MASTER_LOG_FILE='your_current_replog.000693', MASTER_LOG_POS=certain_pos (I used 4);
Good luck!

2011/10/04

Three Steps to Setup SMTP Mail in Debian

Step one

Install Pear (PHP Extension and Application Repository) by the following command,
apt-get install php-pear

Step two

Install Pear Mail Package by the following command,
pear install Mail-1.2.0
Note: the latest version of Mail package at the time I am writing is 1.2.0, check this page for the current version.

Setp three

Install Net_SMTP, which is required to send SMTP mail. You can use the following command to install it.
pear install Net_SMTP

Done!

The following is the script you can use to test,
<?php
include(\"Mail.php\");

function sendmail_smtp($to, $subject, $body){

   $from = \"you@gmail.com\";

   $host = \"ssl://smtp.gmail.com\";
   $port = \"465\";
   $username = \"you@gmail.com\";
   $password = \"yourpass\";

   $headers = array ('From' => $from,
     'To' => $to,
     'Subject' => $subject);
   $smtp = Mail::factory('smtp',
     array ('host' => $host,
       'port' => $port,
       'auth' => true,
       'username' => $username,
       'password' => $password));

   $mail = $smtp->send($to, $headers, $body);

   if (PEAR::isError($mail)) {
      echo(\"<p>\" . $mail->getMessage() . \"</p>\");
   } else {
      echo(\"<p>Message successfully sent!</p>\");
   }

}
?>
Now, you can use Pear SMPT to send mail on any server even your home server without seting up a mail server. Is it cool?