Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Add Comment
Please, Sign In to add comment