MySQL extension
PHP's procedural MySQL extension is the oldest and most common method for interfacing PHP with MySQL. The below code snippet gives an example of how to connect to a MySQL database and run a query using PHP's MySQL extension:
Expand to view code snippetExpand <?php
mysql_connect("localhost", $mysql_username, $mysql_password);
mysql_select_db($mysql_database);
$query = mysql_query("SELECT username, password FROM accounts");
while ($row = mysql_fetch_assoc($query)) {
echo $row['username'] . ": " . $row['password'] . "<br/>";
}
?>
For more information on PHP's MySQL extension, see its PHP manual page.
MySQL Improved (MySQLi) extension
The MySQLi extension is a newer MySQL library for PHP. It offers increased performance as well as an optional object-oriented syntax. The below code snippet gives an example of how to connect to a MySQL database and run a query using PHP's MySQLi extension in an object-oriented fashion:
Expand to view code snippetExpand <?php
$connection = new mysqli("localhost", $mysql_username, $mysql_password, $mysql_database);
$result = $connection->query("SELECT username, password FROM accounts");
while ($row = $result->fetch_assoc()) {
echo $row['username'] . ": " . $row['password'] . "<br/>";
}
?>
For more information on PHP's MySQLi extension, see its PHP manual page.
PHP Data Objects (PDO) extension
The PDO extension is a standarized way for PHP code to interface with database drivers. The below code snippet gives an example of how to connect to a MySQL database and run a query using PHP's PDO extension compiled with MySQL support:
Expand to view code snippetExpand <?php
$connection = new PDO("mysql:dbname=" . $mysql_database, $mysql_username, $mysql_password);
foreach ($connection->query("SELECT username, password FROM accounts") as $row) {
echo $row['username'] . ": " . $row['password'] . "<br/>";
}
?>
For more information on PHP's PDO extension, see its PHP manual page.
MySQL Connector/NET
The .NET Framework's method for interfacing with database engines is called ADO.NET. HelioHost offers the MySQL Connector for .NET, allowing ASP.NET to interface with MySQL through ADO.NET-compatible libraries. The below code snippet gives an example of how to databind a MySQL table to an ASP.NET GridView control using MySQL Connector/NET:
Expand to view code snippetExpand <asp:SqlDataSource ID="DataSource" runat="server" ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT username, password FROM accounts"
ConnectionString="Server=localhost;Database=your_mysql_database;
User=your_mysql_user;Password=your_mysql_password;" />
<asp:GridView runat="server" DataSourceID="DataSource" />
For more information on the Connector/NET extension, see its MySQL manual page. For more information on how ASP.NET works with ADO.NET, see MSDN's "Implementing a .NET Data Provider" tutorial.
MySQL Connector/ODBC
The .NET Framework offers compatibility with ODBC, a standard library for interfacing with database engines. HelioHost offers the MySQL Connector for ODBC, allowing ASP.NET to interface with MySQL through ODBC. The below code snippet gives an example of how to databind a MySQL table to an ASP.NET GridView control using MySQL Connector/ODBC:
Expand to view code snippetExpand <asp:SqlDataSource ID="DataSource" runat="server" ProviderName="System.Data.Odbc"
SelectCommand="SELECT username, password FROM accounts"
ConnectionString="DRIVER={MySQL ODBC 3.51 Driver};Server=localhost;
Database=your_mysql_database;User=your_mysql_user;Password=your_mysql_password;Option=3;" />
<asp:GridView runat="server" DataSourceID="DataSource" />
For more information on the Connector/ODBC extension, see its MySQL manual page. For more information on how ASP.NET works with ODBC, see MSDN's "Implementing a .NET Data Provider" tutorial.
MySQL RubyGem
Ruby extensions are called RubyGems, and the MySQL RubyGem adds a library for interfacing with MySQL. HelioHost offers version 2.7 of the MySQL RubyGem. The below code snippet gives an example of how to connect to a database and run a query using the MySQL RubyGem:
Expand to view code snippetExpand require "rubygems"
require "mysql"
connection = Mysql.new('localhost', mysql_user, mysql_password, mysql_database)
query = connection.query("SELECT username, password FROM accounts")
query.each_hash { |row| puts row['username'] + ": " + row['password'] + "<br/>" }
ActiveRecord
ActiveRecord is a RubyGem that implements object-relationship mapping for the Ruby on Rails application framework. ActiveRecord offers compatibility with MySQL. The below code snippet gives an example of how to connect to a database and run a query using the ActiveRecord RubyGem:
Expand to view code snippetExpand require "rubygems"
require "active_record"
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => mysql_user,
:password => mysql_password,
:database => mysql_database
)
class Account < ActiveRecord::Base
end
Account.all.each { |row| puts row['username'] + ": " + row['password'] + "<br/>" }
For more information on the ActiveRecord RubyGem see its documentation here.
MySQLdb
MySQLdb is a Python library for interfacing with the MySQL database engine. The below code snippet gives an example of how to connect to a database and run a query using the MySQLdb Python library:
Expand to view code snippetExpand import MySQLdb
connection = MySQLdb.connect(
host = "localhost",
user = mysql_user,
passwd = mysql_password,
db = mysql_database
)
cursor = connection.cursor()
cursor.execute("SELECT username, password FROM accounts")
while 1:
row = cursor.fetchone()
if row == None:
break
print row[1] + ": " + row[2] + "<br/>"
For more information on the MySQLdb library, see its documentation or this tutorial
MySQL Connector/J
Java's method for interfacing with database engines is called JDBC. HelioHost offers the MySQL Connector for Java, allowing Java to interface with MySQL through JDBC-compatible libraries. The below code snippet gives an example of how to connect to a database and run a query using the MySQL Connector/J library in JSP:
Expand to view code snippetExpand <%@page import="java.sql.*" %>
<%
Connection connection = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1/" + mysql_database,
mysql_username,
mysql_password
);
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery("SELECT username, password FROM accounts");
while (result.next()) {
%>
<%= result.getString("username") %>: <%= result.getString("password") %><br/>
<% } %>
MySQL library
The MySQL library for Perl enables Perl to communicate to the MySQL database engine. The below code snippet gives an example of how to connect to a database and run a query using the MySQL library for Perl:
Expand to view code snippetExpand use Mysql;
$connection = Mysql->connect("localhost", $mysql_database, $mysql_username, $mysql_password);
$query = $connection->query("SELECT username, password FROM accounts");
while(@row = $query->fetchrow) {
print $row[1] . ": " . $row[2] . "<br/>";
}
DBD::MySQL
Perl's standard for communicating with databases is called the Database Interface, or DBI. Using Database Drivers (or DBDs), DBI is able to communicate to database engines in a standarized way. The MySQL DBD enables DBI to communicate to the MySQL database engine. The below code snippet gives an example of how to connect to a database and run a query using the MySQL DBD:
Expand to view code snippetExpand use DBI;
$connection = DBI->connect("dbi:mysql:".$mysql_database, $mysql_username, $mysql_password);
$query = $connection->prepare("SELECT username, password FROM accounts");
$query->execute;
while(@row = $query->fetchrow_array) {
print $row[1] . ": " . $row[2] . "<br/>";
}
|