PHP cannot connect to MS SQL Server 2008 IIS - php

I did an express install for MS SQL Server 2008 and created a database with a test table. I am running Windows Server 2008 and have IIS, PHP and MS SQL installed. I chose to go with SQL server authentication, where you would need credentials to modify or view the database. I try to print out a simple test table on a web page, but it keeps failing and only returns a white screen..
<?php
$server = "WS1\SQLEXPRESS";
$username = "sa";
$password = "password";
$db = "testdb1";
$dbhandle = mssql_connect($server, $username, $password)
or die ("Cannot connect to SQL Server on $server");
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
echo "You are connected to the " . $db . "database on the " . $server . ".";
$query = "SELECT * FROM table1";
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
echo "<li>" . $row[""] . $row[""] . "</li>";
while ($row = mssql_fetch_array($result)) {
print_r($row);
}
mssql_close($dbhandle);
?>

Ah, the horrific white screen. Sometimes very hard to troubleshoot. Often times it's a simple missing semicolon ...
like the one here:
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
Put a semicolon on the end of that statement and you are back in business.

Related

remote mysql database via php

Hello I have problem with my local computer. I'm install fedora and i have problem to connect with my remote mysql server via php.
php code:
$servername = "my_own_ip";
$username = "test";
$password = "test";
$dbname = "test";
$link = mysql_connect($servername, $username, $password);
if (!$link) {
die('Could sdf connect: ' . mysql_error());
}
echo 'Connected successfully';
or
$servername = "my_own_ip";
$username = "test";
$password = "test";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
I'm sure that my server is correct configured, because I'm tested it before on ubuntu, and now I'm test this script on:
http://phpfiddle.org/
And work normally. I think it is a problem with Fedora system, Can you help me ?
It's can be couple of issues:
Maybe you have a network issue- try to send ping to the server which you want to connect to.
Maybe the firewall of your fedora is on. Turn it off:
$ systemctl disable firewalld
Which error do you get when you run this script?

PHP connection to MS SQL

I'm trying to connect my PHP webpage with my MS SQL database. I've got this code from the internet, but have tried others. All seems to come back with a problem with "mssql_connect".
I have tried (I think) everything and can not find out why it won't work.
My code is:
<?php
$myServer = 'SQL5008.Smarterasp.net,1433';
$myUser = '*****';
$myPass = '*****';
$myDB = '*****';
//connection to the database
$dbhandle = mssql_connect($myServer, $myuser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT id ";
$query .= "FROM tblEmployees ";
$query .= "WHERE CompanyID=3";
//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 "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
Why dont you try using PDO. I use it with SQL SERVER daily. You will need the php sqlsrv extenstion though.
// instantiate the pdo object
try {
$Server = "localhost";
$User = "username";
$Pass = "password";
$Database = "mydb";
$this->conn = new PDO("sqlsrv:Server=$Server;Database=$Database", $User, $Pass);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//allow for SQL query errors
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Check using phpinfo() if mssql extension is loaded. According to PHP Manual:
This extension is not available anymore on Windows with PHP 5.3 or later.
If you find out mssql is unloaded, try to connect using sqlsrv extension. Here you can find a few examples http://php.net/manual/ru/function.sqlsrv-connect.php
Anyway it's a good idea to post here an error message you get from PHP.

"PHP Fatal error: Call to undefined function mssql_select_db() in c:\...appscript.php on line 16"

Folks, Im trying to run a basic php script in cmd prompt which connects to sql server 2012 on my local machine, its gives me the following error..."PHP Fatal error: Call to undefined function mssql_select_db() in c:...appscript.php on line 16"
Here is my script, im not very familiar with php but have been playing around with it and im unsure if my script is correct..
<?php
$Server = "";
$User = "";
$Pass = "";
$DB = "";
//$SQLKEY = "";
//connection to the database
//$dbconn = sqlsrv_connect($Server, $User, $Pass)
$connectionInfo = array("UID" => $User, "PWD" => $Pass, "Database" => $DB);
$conn = sqlsrv_connect( $Server, $connectionInfo);
//or die("Couldn't connect to SQL Server on $Server");
//select a database to work with
$selected = mssql_select_db($DB, $connectionInfo)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT name from test ";
//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);
?>
Be great if someone could help or least give me a few pointers.
It seems you are using wrong functions. You should use only functions from http://www.php.net/manual/en/function.sqlsrv-begin-transaction.php
It seems there is no need to select db (there is no sqlserv_select_db function).
You should change your mssql_query to sqlsrv_query and the same for other functions and you should look at PHP manual because some functions may missing.
You need to enable the MSSQL extension.
http://www.php.net/manual/en/mssql.installation.php (dead link)
Last working version captured by archive.org:
https://web.archive.org/web/20200920214312/http://www.php.net/manual/en/mssql.installation.php

Connection to MySQL using ODBC driver in PHP code

I have a simple code for selecting data using MySQl and PHP.
Here it is:
<?php
require_once 'login.php' ;
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die ("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to choose DB: " . mysql_error());
$query = "SELECT * FROM bank";
$result = mysql_query($query);
if (!$result) die ("Failed with connection to DB: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j=0; $j < $rows; ++$j) {
$row = mysql_fetch_row($result);
echo 'MFO: ' . $row[0] . '<br/>';
echo 'NAME: ' . $row[1] . '<br/>';
echo 'STATE: ' . $row[2] . '<br/><br/>';
}
?>
But i have to use ODBC driver instead of standard integrated driver.
I've read a lot of information but haven't find anything , so maybe someone help what should i add or rewrite in this code for using ODBC ?
There are a lot of odbc functions in PHP.
http://php.net/manual/en/function.odbc-connect.php
You can connect with this functions to mysql and other databases. In the commments on php.net you can find some examples how to connect to MySQL.
http://www.mysql.de/products/connector/
Here you can find an ODBC driver for MySQL.

Php MSSQL(2012) giving error:Couldn’t connect to SQL Server

I am trying to fetch a set of fields from a Database on MSSQL Database on SQL Server 2012. This is a remote server and I am trying the following piece of code.
//MSSQL Server for retrieving the Member name from Member ID:
//mssql.secure_connection = On
// Need to upload ntwdblib.dll from net
$myServer = "IPAddress/SQLExpress "; // host/instance_name
$myUser = "ID"; // username
$myPass = "pass"; // paasword
$myDB = "dbname"; // database name
// connection to the database
$dbhandle = mssql_connect($myServer, $myUser,$myPass)
or die("Couldn’t connect to SQL Server on $myServer"). mssql_get_last_message();
// select a database to work with
$selected = mssql_select_db("sportsclub", $dbhandle)
or die("Couldn’t open database $myDB");
echo "You are connected to the " . $myDB . " database on the " . $myServer . ".";
$query = "SELECT first_name, last_name";
$query .= "FROM members ";
$query .= "WHERE member_id='".$row['member_id']."'";
// your database query
$result = mssql_query($query);
while($row = mssql_fetch_assoc($result))
{
echo "<td>" . $rows["first_name"] . $rows["last_name"] . "</td>";
}
// close the connection
mssql_close($dbhandle);
//Ended MSSQL Connection
It simply does not connect to the sql server. It gives the error: Couldn’t connect to SQL Server on IPAddress/SQLExpress
I tried checking all configurations like TCP/IP through SQL Server Config management.
Can someone please help?
Ensure remote connections over named pipes are enabled and make sure you're using a backslash before the instance name:
$myServer = "IPAddress\SQLExpress ";
If you'd like to enable connection over the default port (1433) this answer will help.

Categories