I'm struggling to connect to my MS SQL Express 2008 edition from my PHP files, but it seems virtually impossible.
I've found several guides/notes on teh intarweb solving the issues, but none of these have helped me any further.
I've taken a script from another site and entered my database information, but I still get errors:
<?php
$myServer = "localhost";
$myUser = "demo_basic";
$myPass = "1234";
$myDB = "demo_basic";
//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 nitid, nisname ";
$query .= "FROM navitems";
//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["nitid"] . $row["nisname"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
I get this error:
Warning: mssql_connect()
[function.mssql-connect]: Unable to
connect to server: localhost in
C:\inetpub\vhosts\dexterholding.dk\httpdocs_rip\sql.php
on line 8 Couldn't connect to SQL
Server on localhost
You can see for yourself at: http://www.dehold.net/_rip/sql.php
Any ideas?
I'm running Windows Server 2008 with PLESK and PHP5 as FastCGI.
I found this guide which actually made it work for me:
http://samsami2u.wordpress.com/2008/06/30/how-to-connect-mssql-with-php/
With MSSQL, your server name will look like this: machinenameoraddress\serverinstancename
and example would be 192.168.14.201\MSSQLEXPRESS or TESTMACHINE\MYTESTDBSERVER.
It could be that the user account does not have permission to access SQL Server.
Although looking at your code you are using SQL auth and not Windows authentication, presumably this account is set up in SQL Server and it is configured to allow SQL Auth?
Related
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
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.
#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.
I'm trying to access and read a view from a MS SQL database with PHP. For now I'm just trying to echo out the results on the screen. When I test, my page shows nothing at all.
This is what I have:
<?php
$myServer = "localhost";
$myUser = "username1";
$myPass = "password1";
$myDB = "database1";
//connect to database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select database
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare statement
$query = "SELECT ProductId";
$query .= "FROM Inventory ";
$query .= "WHERE UPC='15813658428' and ManufacturerID=465";
//execute the query
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close
mssql_close($dbhandle);
?>
Often when you get a completely blank output from PHP, it is because the server encountered a problem when parsing or interpetting your code. The best way to deal with this when you are getting no other errors is to strip out (or comment out) large blocks of your code and to use numeric echo statements to verify that the code you have not stripped out is executing. Then slowly add more and more of the code back in.
In this specific case, I am guessing that the problem may come down to the spaces after the ?> in your php file. That can confuse servers. Try removing the ?> completely.
If that still results in no output, then comment out everything after $myDB = "database1";, add an echo statement right before the commenting, and slowly move code back into the uncommented section until you find the line that causes the problem.
We have a database running on Windows Server 2008 R2, SQL Server 2008 R2. I need to query the database via PHP (I'm also open to other options) and display the data in a web page.
I have tested connectivity with my username, password and server name using a UDL test file. My connection is successful.
My web server has PHP 5.4 installed and working.
I have built a PHP script that is supposed to connect and query the database but I get a server error.
<?php
$myServer = "ipaddress";
$myUser = "username";
$myPass = "password";
$myDB = "dbname";
//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 fullName, email_address, pos_desc";
$query .= " FROM dbo.EMPLOYEE_VIEW ";
$query .= "WHERE grp_cde='STAFF'";
//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);
?>
Keep in mind the Database server and the Web Server are both running Windows Server 2008 R2 and exist in the same domain.
The only error I receive is:
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
Is there anything I can do to troubleshoot why this is not working?
I appreciate any and all help on this question.
Error 500 usually means you forgot a ; or ', I suggest you check your code carefully for any redundant or missing characters.
Also, turning on php errors in your php.ini file helps alot with fixing these sort of stuff.
After pheww... what now? 6 hours? I have it working! The problem was that I needed to set up SQL Server 2008 drivers on my web server and connect using ODBC via my PHP script. I am now pulling and displaying data from my SQL Server 2008 Database like a pro. Thanks for the assistance guys.
odbc_connect("DRIVER={SQL Server Native Client 10.0};Server=serverip;Database=tablename", "username", "pass");
Can any one explain the connection string from the parameters below:
server IP : 192.168.137.4
Windows Authentication : Windows Authentication
UserName : DELL-M102Z\dell
Database : DataProd
Network Protocol : <default>
Product Name : Microsoft SQL Server Express Edition
Server Name : DELL-M102Z\SQLEXPRESS
Instance Name : SQLEXPRESS
Computer Name : DELL-M102Z
I tried:
$serverName = "DELL-M102Z\SQLEXPRESS"; //serverName\instanceName
$username = "DELL-M102Z\dell"; //serverName\instanceName
$conn = mssql_connect( $serverName,$username,'');
...but the result I got was:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: DELL-M102Z\SQLEXPRESS in C:...\index.php on line 17
Connection could not be established.
Can anyone tell me what is the problem here?
trying to follow this step:
http://michaelellerbeck.com/2010/03/31/cant-connect-remotely-to-sql-server-2008/
i know that i miss to activate sql browser service, and that's why i cannot connect by remote IP,
I got conclusion from that site and i must make sure that:
1. make sure you have allow network connection from sql server configuration tool
2. allow connection for this port in firewall
3. activate sql browser service
4. make sure port is listen as the the service provide
i got problem for no.3 and i'm stuck on it, this site solve the problem:
http://www.wikihow.com/Enable-Remote-Connections-SQL-2008-Express
<?php
$myServer = 'xxx.xxx.xxx.xxx:yyyy';
$myUser = 'sa';
$myPass = 'xxxxx';
$con = mssql_connect($myServer, $myUser, $myPass) or die("Could not connect to database: ".mssql_get_last_message());
if($con){
echo "connected";
}
// Select a database:
mssql_select_db('new')
or die('Could not select a database.');
// Example query: (TOP 10 equal LIMIT 0,10 in MySQL)
$SQL = "SELECT TOP 10 * FROM Table";
// Execute query:
$result = mssql_query($SQL)
or die('A error occured: ' . mysql_error());
// Get result count:
$count = mssql_num_rows($result);
print "Showing $count rows:<hr/>\n\n";
// Fetch rows:
while ($Row = mssql_fetch_assoc($result)) {
print $Row['BillNo'] . "\n";
}
mssql_close($con);
$myServer = 'IP_Address:PORT';
Here semicolon is need to ip and port
and
Mssql is enbaled at your cpanel
So a friend of mine and I are using both xampp on ubuntu, if that helps, to connect between each other's website, We both created the same php file to connect, so we use de IP of the other, but then it says an error
Warning: mysql_connect() [function.mysql-connect]: Host 'coke-laptop.local' is not allowed to connect to this MySQL server in /opt/lampp/htdocs/connection.php on line 2
Could not connect: Host 'coke-laptop.local' is not allowed to connect to this MySQL server
We have this code on the connection.php file:
<?php
$link = mysql_connect('10.100.161.37','root','');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//echo 'Connected successfully';
$db_selected = mysql_select_db('Prueba', $link);
if (!$db_selected) {
die ('Can\'t use Prueba : ' . mysql_error());
}
// This could be supplied by a user, for example
$firstname = 'fred';
$lastname = 'fox';
// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT * FROM Agencia");
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['ID'] . " ";
echo $row['Nombre'] . "\n\r";
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
mysql_close($link);
?>
If we use the IP just like that, we can enter each others xampp normal welcome page.
Check you have enabled remote access to the MySQL server. Open the my.cnf file (probably found inside xampp/etc/), go to the [mysqld] section and add the following (using your own ip address instead of the example)
bind-address=192.168.1.100
If there is a line that says skip-networking, comment that out so it looks like this:
# skip-networking
then restart the MySQL server
It looks like your MySQL database isn't allowing you to connect remotely with the credentials you provided. You will need to configure a remote user to connect. Try looking into MySQL Grants.
For Example:
GRANT SELECT, INSERT ON database.* TO 'someuser'#'somehost';