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");
Related
I'm relatively new to any type of programming or coding. I'm not quite understanding why no matter what adjustments I make to my php file I can't seem to pull any data from a table.
Here is a link to the table: https://i.gyazo.com/4ad5e860895014c49dbe0539c38cdec2.png
Above is the test table I have been trying to use. From what I can understand I'm connecting to the database okay, but all of my problems come after the connection. Also, I'm using php 7.0 so a lot of the information I'm finding online has not been helpful.
If there is something glaringly wrong with my table or in my code, please let me know.
Here is my code:
'''
//Set Variables
$serverName = "localhost";
$userName = "root";
$password = "";
$databaseName = "test";
//Create Connection
$connection = mysqli_connect($serverName,$userName,$password,$databaseName);
//Check Connection
if(!$connection){
die("Connection failed: ".mysqli_connect_error());
}
echo "Connected successfully <br>";
//Fetch Data
$query = "SELECT * from table1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ($row[1], $row[2]);
mysqli_free_result($result);
mysqli_close($connection);
I figured out the issue a few hours ago. The code I had posted would have worked perfectly fine if I was connecting to the port for MySQL rather than MariaDB. Didn't realize that the port that MariaDB was connected to was the default.
MariaDB by default was port 3306, but MySQL was 3308. After specifying 'localhost:3308' I was able to start properly pulling rows from my tables.
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.
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
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?