Kris Edwards
Kris Edwards

Reputation: 35

Issues with PHP connection script to MSSQL database

Good morning,

I am quite new to php and I am trying to create a connection to a MSSQL server, I've been able to do it through MYSQL php connection but what I thought would be a simply change to MSSQL is proving to be much harder than expected.

The below code is basically what I am using after much googleing and search in this website this is what i've come up with:

<?php
$Server = "127.0.0.1";
$User = "BOB123";
$Pass = "BOBPASS";
$DB = "BOBDB";

//connection to the database 
$dbconn = mssql_connect($Server, $User, $Pass)
or die("Couldn't connect to SQL Server on $Server");

//select a database to work with
$selected = mssql_select_db($DB, $dbconn)
or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT CustomerName from tblCustomer ";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
echo "<br>" . $row["name"];
}
//close the connection
mssql_close($dbconn);
?>

As you can see the above script is very basic and there are very similar ones knocking around on the web could anyone help in connecting to the server this script doesn't seem to want to connect. I've changed the log on details as you'd probably know.

Thanks

Kris

Upvotes: 0

Views: 143

Answers (2)

tail_recursion
tail_recursion

Reputation: 740

@Daniel Gelling Doesn't look like a typo, looks like he is trying to connect to Microsoft SQL Server using mssql. You are correct about the API being outdated however.

Upvotes: 0

Daniel Gelling
Daniel Gelling

Reputation: 920

You have a typo on:

$dbconn = mssql_connect($Server, $User, $Pass);

Should be:

$dbconn = mysql_connect($Server, $User, $Pass);

You're typing mysql wrong on each mysql_ function you create, change all mssql_ to mysql_

Note:

You shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi.

Upvotes: 2

Related Questions