Connecting to MSSQL in PHP without a DSN

This script demonstrates connecting to a MSSQL database in PHP without a DSN. The script will make the connection and then output all the rows of a specified table. Any lines beginning with a // are comments.

$hostname = "mssql1.easycgi.com";
Sets a variable to hold the address of the MSSQL server.

$database = "testdatabase";
Sets a variable to hold the name of the database. You should change this to the name of your MSSQL database.

$tablename = "testtable";
Sets a variable to hold the name of the table to display. You should change this to one of the tables in your database.

$userid = "testuser";
$password = "testpass";

These hold the username/password for your MSSQL database.

$dbc = mssql_connect($hostname, $userid, $password) or die("Connection error! Host: " . $hostname);
This opens a connection to the database. If there is an error, it is printed and the program exits.

mssql_select_db($database, $dbc) or die("Select db error! DB Name: " . $database );
We select the database to use on the MSSQL server. This is done so that we do not have to specify the database name in the other commands.

$rs = mssql_query("SELECT * FROM $tablename", $dbc) or die("Invalid query!");
Execute a query on the database and store the results in $rs.

print "<TABLE BORDER=1>";
Starts an HTML table.

if( mssql_num_rows($rs) > 0 ){
If there was a result.

print "<TR>";
Starts a new row in the HTML table.

$numfields = mssql_num_fields($rs);
Gets the number of fields in the result set.

for($loop = 0; $loop < $numfields; $loop++){
//print the name of each field
$fld = mssql_field_name($rs, $loop);
print "<TH>$fld</TH>";
}

Loops through each of the fields and print out the field name as the first row in the HTML table.

print "</TR>";
Ends the HTML row.

while( $row = mssql_fetch_row($rs) ) {
Goes though each row in the result set.

$numfields = mssql_num_fields($rs);
Gets the number of fields in the result.

print "<TR>";
Starts a new HTML row.

for($loop = 0; $loop < $numfields; $loop++){
//print each field's value
$fld = $row[$loop];
print "<TH>$fld</TH>";
}

Goes through all of the fields in the table and print them in the HTML table.

print "</TR>";
Ends the HTML row.

print "</TABLE>";
Ends the HTML table.

mssql_close($dbc);
Closes the connection to the database.

--Here is the full script, the filename should be: mssql-dsnless.php--

<html>
<head>
<title>Sample script for accessing a MSSQL database in PHP without a DSN</title>
</head>
<body>

<?php

//Easy CGI Test script for connecting to a MSSQL database in PHP without a DSN
//This script will connect to the database and output the results in an HTML table

// Change these values to point to your database
// --------------------------------------------
// the host name for the mysql server - should be IP address - hostname does not always work
$hostname = "216.150.152.26";
// the database name
$database = "testdatabase";
// the name of the table to display
$tablename = "testtable";
// username and password for the database
$userid = "testuser";
$password = "testpass";
// ----------------------------------------------

//connect to database
$dbc = mssql_connect($hostname, $userid, $password) or die("Connection error! Host: " . $hostname);

//select the database
mssql_select_db($database, $dbc) or die("Select db error! DB Name: " . $database );

//execute a query
$rs = mssql_query("SELECT * FROM $tablename", $dbc) or die("Invalid query!");

//print the results to a table
print "<TABLE BORDER=1>
";

//print the column names
if( mssql_num_rows($rs) > 0 ){
print "<TR>";
$numfields = mssql_num_fields($rs);

for($loop = 0; $loop < $numfields; $loop++){
//print the name of each field
$fld = mssql_field_name($rs, $loop);
print "<TH>$fld</TH>";
}
print "</TR>";
}

//while not the end of the result set
while( $row = mssql_fetch_row($rs) ) {
$numfields = mssql_num_fields($rs);

print "<TR>";
for($loop = 0; $loop < $numfields; $loop++){
//print each field's value
$fld = $row[$loop];
print "<TH>$fld</TH>";
}
print "</TR>";
}

print "</TABLE>";

//dont forget to close connection
mssql_close($dbc);

?>
</body>
</html>

--End Script--

<< Back

 
 
 
Copyright © 2004 Creative Brain   || Home | Digital Photo Album | Online Store | Template Depot | Career | Help & Support | Contact Us