Fandom

Programmer's Wiki

MySQL connection

406pages on
this wiki
Add New Page
Talk0 Share
16px-Pencil


C Edit

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

C++ Edit

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

PHP Edit

<?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);
?>

Java Edit

//...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();

Perl Edit

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

Python Edit

   import MySQLdb
   
   connection = MySQLdb.connect(user='myuser', passwd='my password',
                                host='localhost', db='database_name')
   
   cursor = connection.cursor()
   cursor.execute('SELECT * FROM my_table')
   results = cursor.fetchall()
   
   print 'Total of %d rows' % len(results)
   for result in results:
       print result

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.