Raven and MySQL
Just like files, databases are storage containers for data, albeit more complex. Just like files, accessing a database requires opening a resource using a resource descriptor string (a URL in fact) of a particular format. Connect to the database:
'mysql://username:password@localhost:port/database' open as $mysql
Above, the resource descriptor string should look familiar to anyone who has used a URL to surf to a website, particularly an FTP site requiring username/password credentials. It breaks down as follows:
* mysql:// (required) specifies the database driver to use. * username (required). * password (optional). * localhost (required) is the hostname of the server, which could be any name resolvable through DNS or /etc/hosts. Hostname uses an @ symbol to separate the hostname from the password. * port (optional) is the port on which the MySQL server is running. * database (required) is the name of the default database to use.
Once a resource handle has been opened, it may be used to execute queries and return results.
Select data.
'SELECT * FROM mytable' $mysql query as $result
Determine how many rows are available.
$result selected as $selected_rows
Retrieve the next available row as a hash.
$result fetch as $row
Retrieve entire data set a row at a time.
$result each as $row $row print
Modify data.
'UPDATE mytable SET myfield = 3' $mysql query as $result
Determine how many rows were affected.
$result affected as $updated_rows
You can find more about Raven here:
http://mythago.net/samples/mysql.html