Php and Rails handle MySQL queries in two very different fashions. Fundamentally, they’re the same but Rails does a lot of the work for you.
You have probably had to access a MySQL database a million times as a PHP developer, here’s how it would look…
<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');
// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
This is assuming that we have properly setup our database connection and ensured that it is compiled and in working order.
Now, with the same assumptions in Rails I will query the database and output in a similiar fashion.
controller.rb
@models = Model.find(:all)
view.rhtml
<% for row in @models >
< for column_value in row.attributes >
<= column_value %> <% end %><% end %>
Inside of Rails, the data has already been mapped to your variables for you (in Object Oriented fashion).
Bath and Shower
Fragrance
Gift Sets
Hair Care
Makeup
Men’s Grooming
Shaving and Hair Removal
Skin Care
Tools and Accessories
Baby Apparel
Baby Bathing & Skin Care
Baby Bedding
Baby Car Seats
Baby Diapering
Baby Feeding
For Moms
Baby Furniture
Baby Gear
Baby Gifts
Baby Health & Baby Care
Nursery Décor
Potty Training
Baby Safety
Baby Strollers
Php and Rails handle MySQL queries in two very different fashions. Fundamentally, they’re the same but Rails does a lot of the work for you.
You have probably had to access a MySQL database a million times as a PHP developer, here’s how it would look…
<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');
// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
This is assuming that we have properly setup our database connection and ensured that it is compiled and in working order.
Now, with the same assumptions in Rails I will query the database and output in a similiar fashion.
controller.rb
@models = Model.find(:all)
view.rhtml
<% for row in @models >
< for column_value in row.attributes >
<= column_value %> <% end %><% end %>
Inside of Rails, the data has already been mapped to your variables for you (in Object Oriented fashion).
Bath and Shower
Fragrance
Gift Sets
Hair Care
Makeup
Men’s Grooming
Shaving and Hair Removal
Skin Care
Tools and Accessories
Baby Apparel
Baby Bathing & Skin Care
Baby Bedding
Baby Car Seats
Baby Diapering
Baby Feeding
For Moms
Baby Furniture
Baby Gear
Baby Gifts
Baby Health & Baby Care
Nursery Décor
Potty Training
Baby Safety
Baby Strollers