I wrote equivalent programs in Ruby and PHP. I see little difference between the two in terms of "detailedness" to catch errors when they occur. Can someone critique the code and see what I need to do to catch on to "The Ruby Way"? It looks like an oldie-moldy procedural guy writing procedural code in an OO language.
1 #!/usr/bin/env ruby
2 require "mysql"
3 puts "The Cream of the Crop"
4 begin
5 $my = Mysql.connect('localhost', 'xxx','xxx', 'mysql')
6 rescue => err
7 puts "Connection failed: #{err}!"
8 exit
9 end
10 begin
11 res = $my.query("select host,user,password from user")
12 rescue => err
13 puts "No result returned from query: "
14 exit
15 end
16 puts "#{res.num_rows.to_s} row(s)\n"
17 (1..res.num_rows).each {|n|
18 begin
19 row = res.fetch_row
20 rescue => err
21 puts "Fetch failed: $err"
22 quit
23 end
24 printf("%2d %s\n",n,row.inspect)
25 }
1 <?php
2 echo "The Cream of the Crop\n";
3 $conn = mysql_connect('localhost','xxxxx','xxxxxxx');
4 if ($conn == FALSE) {
5 echo 'Connection failed: '.mysql_error($conn);
6 } else {
7 mysql_select_db('mysql', $conn);
8 $query = "select host, user, password from user";
9 $res = mysql_query($query, $conn);
10 if ($res == NULL) {
11 echo 'No result returned from query: '.mysql_error($conn);
12 } else {
13 $rows = mysql_num_rows($res);
14 echo "$rows row(s)\n";
15 $cols = mysql_num_fields($res);
16 for ($row = 0; $row < $rows; ++$row) {
17 $a = mysql_fetch_row($res);
18 echo "$row ";
19 print_r($a);
20 echo "\n";
21 }
22 mysql_free_result($res);
23 }
24 }
25 ?>