Skip to content

Instantly share code, notes, and snippets.

@beweinreich
Created July 17, 2012 14:08
Show Gist options
  • Save beweinreich/3129575 to your computer and use it in GitHub Desktop.
Save beweinreich/3129575 to your computer and use it in GitHub Desktop.
Running a ruby web client using mysql
#!/usr/bin/ruby
require 'rubygems'
require 'socket'
require 'mysql2' # gem install mysql2
webserver = TCPServer.new('127.0.0.1', 6789)
client = Mysql2::Client.new(
:host => "127.0.0.1",
:username => "root",
:password => "root",
:database => "example"
)
records = client.query("SELECT * FROM users")
while (session = webserver.accept)
session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
# print something on the screen
session.print "Hello, world"
request = session.gets
# print out records from the database
records.each {|r| session.print "<p>#{r['name']} - #{r['age']}</p>"}
session.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment