Technology
 

MySQL connection

From Programmer's Wiki

Contents


[edit] C

This article is missing a code example in the C language.

[edit] C++

This article is missing a code example in the C++ language.

[edit] PHP

<?php
$dbhost = 'localhost';  // Name of the server
$dbuser = 'root';       // Username
$dbpass = 'password';   // Password

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'dbname';    // Database Name

mysql_select_db($dbname);
?>

[edit] Java

//...necessary import packages
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; //helpful but not used in this example

//...code within a class
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql:///test","root", "secret");
//...preform actions with the open database connection
con.close();

[edit] Perl

use DBI;
$dbh = DBI->connect('DBI:mysql:databasename;host=db.example.com', 'username', 'password',
	            { RaiseError => 1 }
	           );
# do something with your db...
$dbh->disconnect();

[edit] Python

This article is missing a code example in the Python language.