Connecting remote SQL Server to PHP - php

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

Related

need help traying to access my database with mysql

Just built my first database to test out learning skills using the simple query below:
<?php
mysqli_connect("xxx","yyy","zzz" , "newtone" );
if (mysqli_connect_error()){
echo ("could not connect to database");
}
?>
This is what I got
:(42000/1044): Access denied for user 'yyy'#'xxx' to database 'newtone'......
I don't get it. So When i Use this:
mysqli_connect("xxx","yyy","zzz" );
if (mysqli_connect_error()){
echo ("could not connect to database");
}
?>
I don't get an error message, just a blank page (notice how the database name is not included on the login argument). What am I doing wrong?
You want to connect with database then need three things for create connection dbhost username,dbhost password and your database name.
<?php
$dbhost = "localhost";
$dbuser = "root"; //In case of localhost (default db username)
$dbpass = ""; //In case of localhost
$dbname = "testDb";//Your database name
$con = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname );
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Your 2nd example works because you are successfully connecting to the MySql server, and defaulting to a default database other than "newtone"
If you tried a command like mysqli::select_db after connecting, you would see the same error. http://php.net/manual/en/mysqli.select-db.php
The crux of your problem seems to be permissions for your user yyy. Make sure user yyy has various permissions like read/write on the DB "newtone"

PHP - How do I tell why mssql_connect() didn't work?

I'm using PHP 5.2, and normally mssql_connect works fine - but I'm trying to connect to a new MS SQL server, and it won't connect. I've probably got something wrong in the connection details or credentials, but I have no way of telling as I can't get an error message.
The mssql_connect() method returns false, and no connection is available.
mssql_get_last_message() returns nothing - so how do I tell why my connection failed?
Anyone got any ideas?
In MySQL I'd use mysql_error - but there doesn't seem to be an equivalent for ms_sql.
[EDIT]
This question is not a duplicate of "MSSQL_CONNECT returns nothing - no error but no response either" - I'm using php 5.2, and the code works fine for other connection details. I need to figure out how to output what the connection error is - not what the problem is with connecting.
[EDIT2]
To clarify:
the extension is enabled on the server
the code works fine for existing connection details
I am using new connection details and don't know if the hostname or password is wrong
I know the code isn't connecting because I check to see if it's connected
I want to find out the connection error - like you can with MySQL.
Sample code below:
$db_connection = mssql_connect($host, $user, $pass);
$db = mssql_select_db($dbname, $db_connection);
if (!$db_connection) {
echo "No connection";
echo "connection failed because: " . ???????;
die;
}
What do I put in place of ??????? to get the connection error message?
//Database Connection by PDO (MS SQL Server, PHP)
$serverName='localhost';
$database='TreeDB';
$uid='sa';
$pwd='sa*5234';
$dbo = new PDO("sqlsrv:server=$serverName ; Database = $database;ConnectionPooling=0", $uid, $pwd);
//var_dump($dbo);
//Database Query
$sql = "SELECT node.id, node.name, COUNT(parent.id) - 1 AS depth, node.lft, node.rgt
FROM dbo.OrgTree AS node CROSS JOIN dbo.OrgTree AS parent
WHERE (node.lft BETWEEN parent.lft AND parent.rgt)
GROUP BY node.id, node.name, node.lft, node.rgt
ORDER BY node.lft";
//Execute Query
$sql = $dbo->prepare($sql);
$sql->execute();
$qry = $sql->fetchAll();
foreach ($qry as $fetch)
{
$tree[] = $fetch;
}
//print_r($tree);
See below
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = 'KALLESPC\SQLEXPRESS';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
In this way you can display error if connection fails.
Ref: http://www.php.net/manual/en/function.mssql-connect.php

PHP to query SQL Server 2008 R2

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");

Mysql connect Error

With this code:
mysql_connect("mysql.webzdarma.cz", "octopus74", "*") or die ("Mysql connect Error>");
MySQL_Select_DB("octopus74") or die("Cant choose MySql database.");
It results in: "Mysql connect Error"
Change your die() calls to die(mysql_error()), which will output the ACTUAL reason for the error, which is of far more use than your fixed text.
Source : http://wallstreetdeveloper.com/php-database-connection/
I found a very useful code for connecting with mysql i posted below:
<?php
//Step-1 : Create a database connection
$connection=mysql_connect(“localhost”,”root”,”root”);
if(!$connection) {
die(“Database Connection error” . mysql_error());
}
//Step-2 : Select a database to use
$db=mysql_select_db(“widget_corp”,$connection);
if(!$db) {
die(“Database Selection error” . mysql_error());
}
?>
<html>
<head>
<title>Database</title>
</head>
<body>
<?php
//Step 3 : Perform database Queury
$result=mysql_query(“select * from subjects”,$connection);
if(!$result) {
die(“No rows Fetch” . mysql_error());
}
//Step 4 : Use returned data
while($row=mysql_fetch_array($result))
{
//echo $row[1].” “.$row[2].”<br>”;
echo $row["menu_name"].” “.$row["position"].”<br>”;
}
?>
</body>
</html>
<?php
//Step 5 : Close Connection
mysql_close($connection);
?>
first are you sure that your mysql username and password are correct?
The syntax for mysql connect is:
mysql_connect('your host server', 'mysql_username', 'mysql_password');
The syntax for mysql select db is:
mysql_select_db ('your_database_name');
Are you sure that your mysql username and mysql database name is the same : "octopus74".
I would recommend to do in this way:
$conn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$conn) {
die('Not connected : ' . mysql_error());
}
// select db
$db_selected = mysql_select_db('mydbname', $conn);
if (!$db_selected) {
die ('Cannot use database mydbname : ' . mysql_error());
}
Open up the server's my.cnf and find this line:
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
If it's localhost (127.0.0.1) you won't be able to connect to it. Change it to 0.0.0.0 to allow the server to listen for external connections.
On the other hand, if it's 0.0.0.0 and you can't connect, check that:
Server is up (no laughing matter, I've seen these cases)
Service / Daemon is up
Port is open and you're connecting through the right one: it might have been reassigned.
If all else fails ... use fire and call your SysAdmin.

PHP Can't Connect to MS SQL Express 2008

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?

Categories