Archive for the ‘Beginner’ Category

ASP Database Connection

Tuesday, April 27th, 2010

Below is a simple asp connection script with a query designed to show all the tables held within your database.

Copy and paste this into a asp page, change the connection details to those specified and watch the database return the results.

You can continue to modify this script to run other quires on your database and return the results.

<%
'Dim statement to declare variables
Dim sConnection, objConn , objRS
 
'set database connection variables
username = "enter your username here"
password = "enter your password here"
database = "enter your database name here"
host = "enter your host here"
 
'assign a value to the sConnection variable
sConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER="&host&"; DATABASE="&database&"; UID="&username&";PASSWORD="&password&"; OPTION=3"
 
'create the ADODB object
Set objConn = Server.CreateObject("ADODB.Connection")
 
'open the connection
objConn.Open(sConnection)
 
'excecute mysql query
Set objRS = objConn.Execute("show tables")
 
'loop through the mysql result and display its output
do until objRS.EOF
  for each x in objRS.Fields
    Response.Write(x.value & "<br />")
  next
  objRS.MoveNext
loop
 
'close and unset variables
objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>

PHP Database Connection

Monday, April 26th, 2010

Below is a simple php connection script with a query designed to show all the tables held within your database.

Copy and paste this into a php page, change the connection details to those specified and watch the database return the results.

You can continue to modify this script to run other quires on your database and return the results.

<?php
$database = 'enter your database name here';
$username = 'enter your username here';
$password = 'enter your password here';
$hostname = 'enter your hostname here';
 
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
    die('Not connected : ' . mysql_error());
}
 
// make foo the current db
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
    die ('Can\'t connect : ' . mysql_error());
}
 
// if you've got this far, the database connection has been made
echo "<p>You have successfully connected to $database</p>";
 
// quick script to display all the tables in your database
// run the mysql query and check the result
$result = mysql_query('show tables');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
 
// loop through the result and display
echo "<p>Tables within in $database</p>";
if(is_array(mysql_fetch_array($result))) {
	while ($row = mysql_fetch_array($result)) {
	    echo $row[0] . '<br>';
	}
} else {
	echo "<p>No tables found</p>";
}
?>