March 23rd, 2012, 06:07 AM
-
Problem connecting to database
Hi all,
I'm usually developing using MySQL but now I got a project using MSSQL and having trouble even at the first hurdle.
I got a config.php file:
PHP Code:
$hostName = "csfdb01";
$databaseName = "CSF_Intranet";
$username = "x";
$password = "x";
I know the username and password are correct.
Then I have a page with a include for the config file and bit of code and the rest of the page is html.
When I refersh the page the php code at the top is stopping the html from showing, so wondered if somebody could have a look and see if there any code issues, or ways of seeing if Im connecting to the database OK, as no errors are showing up either.
Code:
<?php
include "config.php";
if (!($connection = @ mssql_connect($hostName, $username, $password)))
die("error connecting");
if (!mssql_select_db($databaseName, $connection))
die("no db");
?>
<html>
<head>
<title>CSF Admin</title>
</head>
<body link="white" vlink="white">
<div align="center">
<p>
<font size="2" color="#990000" face="Verdana,Arial,Helvetica,sans-serif">Website Master - administration panel</font>
</p>
<div align="center"><font face="tahoma" color="#FF0000" size="2"><b><?php echo $error1; ?></b></font></div>
<table align="center" width="371" bordercolor="#00CCFF" bordercolordark="#0099FF" border="0" cellpadding="5" cellspacing="2">
<tr>
<td width="50%" height="45" style="background-image:url(images/admin_Grey_Bar_Small.jpg)">
<div style="font-family:Verdana, Arial, Helvetica, sans-serif; position:relative; font-size:13px; color:#ffffff; font-weight:bold; position:relative; float:left; left:20px; top:-2px;">
<?
function welcome(){
if(date("H") < 12){
return "Good morning - Log in below.";
}elseif(date("H") > 11 && date("H") < 18){
return "Good afternoon - Log in below.";
}elseif(date("H") > 17){
return "Good evening - Log in below.";
}
}
echo welcome();
?>
</div>
</td>
</tr>
<tr>
<td bgcolor="666666" width="350" valign="top">
<div align="center" style="width:350px">
<form action="index.php" method="post" name="FormName">
<table width="322" border="0" cellspacing="10" cellpadding="0">
<tr>
<td><font size="2" color="#ffffff" face="Verdana,Arial,Helvetica,sans-serif">User:</font></td>
<td><input type="text" name="txtuser" size="24"></td>
</tr>
<tr>
<td><font size="2" color="#ffffff" face="Verdana,Arial,Helvetica,sans-serif">Password:</font></td>
<td><input type="password" name="txtpass" size="24"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="login" value="Log In"></td>
</tr>
</table>
</form>
<table>
<tr>
<td width="350">
<div style="position:relative; font-size:11px; color:#ffffff; float:right; width:350px; font-family:Verdana, Arial, Helvetica, sans-serif; text-align:right;">CMS V.1</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
</tr>
</table>
</div>
</body>
</html>
March 23rd, 2012, 07:27 AM
-
I thought i would try and change the code and see if its that, againusing the config.php file, and the same happens where nothing beneath that php will display, the page is completely empty with no errors.
here is the new page:
PHP Code:
<?php
include "config.php";
// connection to the database
$dbhandle = mssql_connect($hostName, $username, $password)
or die("Couldn’t connect to SQL Server on $hostName");
// select a database to work with
$selected = mssql_select_db($databaseName, $dbhandle)
or die("Couldn’t open database $myDB");
echo "You are connected to the" . $databaseName . "database on the" . $hostName . ".";
$query = "SELECT * FROM Contract_Number"; // your database query
$result = mssql_query($query);
while($row = mssql_fetch_assoc($result))
{
print_r($row);
}
// close the connection
mssql_close($dbhandle);
?>
Then
<html>
</html>
is beneath that but nothing benath that php will show, and it wouldnt be so bad if an error showed up but there nothing, if its not the code what could it be.
March 23rd, 2012, 05:24 PM
-
Read FAQ #30; it's also applicable to "my page is blank below a certain point".
Most likely you don't have mssql extension installed/enabled.
PHP FAQ
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around