Accessing Remote MSSQL Server from hosted linux PHP - php

I've seen a number of questions asking similar but these are usually answered by having access to the server to install extra packages such as FreeTDS. We don't have such access as out linux server is hosted with 1and1.
My code (from an earlier question by someone else on here) is:
try {
$hostname = "xx.xx.xx.xx";
$port = xxxxx;
$dbname = "ClientDatabase";
$username = "uuuu";
$pw = "pppp";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
The error message I get is:
Failed to get DB handle: could not find driver
The MSSQL server is accessible so I can add features to that if necessary.
The MSSQL server is also running IIS7 but doesn't run PHP. I know very little about IIS7 but would it be easier to run the PHP scripts on there rather than the hosted linux box?
Could anyone advise if I can actually connect to the MSSQL server in anyway?
Thanks in advance,
Dave

Have you tried mssql_connect?
Here's the syntax:
mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]] )
In your case it would be:
$link = mssql_connect($hostname, $username, $pw);
mssql_select_db($dbname, $link);
Note that $hostname should contain MSSQL Instance Name. e.g. 'KALLESPC\SQLEXPRESS'

After much trial and error I'm in.
try {
$hostname = "localhost";
$port = 123456;
$dbname = "ClientDatabase";
$username = "uuuu";
$pw = "pppp";
$dbh = new PDO ("sqlsrv:Server=$hostname,$port;Database=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
unset($dbh); unset($stmt);
Thanks to those who answered and those who answered similar questions by others. Got there.... eventually.
Cheers
Dave

Related

PDO PgSQL sometimes doesn't connect

This is a awkward issue, and I'm running out of alternatives to workaround.
I have a server running Wamp 3.0.6, with PHP 5.6.
My code must connect in a MySQL (local) and a PostgreSQL (remote server).
Everything is working fine, and seemingly out of nowhere it stops returning data from PG. After few seconds or minutes, it just works again.
Even when I'm not able to get data from PG, phpPgAdmin keeps working.
Here is my connection function:
function pdo_pgsql($sql){
$host = '000.000.000.000';
$user = 'user';
$pass = 'pass';
$db = 'db';
try {
$PDO = new PDO( 'pgsql:host=' . $host . ';dbname=' . $db . ';port=5432', $user, $pass, array(
PDO::ATTR_PERSISTENT => true
));
}
catch ( PDOException $e ) {
echo 'Error: ' . $e->getMessage(); exit;
}
$result = $PDO->query( $sql );
if (is_array($result)){
$row = $result->fetchAll( PDO::FETCH_ASSOC );
}else{
$row = $result;
}
return $row;
}
Any suggestion to help me with this?
Thanks
I have the same problem, and I found the solution, it is the PDO::ATTR_PERSISTENT need to be set to false.
This fixed my problem.

Connection between PHP and SQL server using WAMP new approaches

what is the best way for create a connection between PHP and SQL server that are seperate?(two server: server a SQL and server b PHP)
notice that I use wamp.
I read some articles like below but I want to know is there any new idea?
I test this code that works perfectly:
try{
$user = 'user';
$password = 'pass';
$server="localhost";//or server IP
$database="database";
$conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $password);
} catch (PDOException $e) {
echo "Failed : " . $e->getMessage() . "\n";
exit;
}
I use PDO_ODBC Method:
1- Install ODBC on server that has wamp and enable PHP_PDO_ODBC extension on your wamp
2- Use this code that supports UTF-8:
try{
$hostname = "IP";
$dbname = "database";
$username = "username";
$pw = "password";
$pdo = new PDO ("odbc:Driver={SQL Server Native Client 10.0};Server=$hostname;Database=$dbname; Uid=$username;Pwd=$pw;");
} catch (PDOException $e) {
echo "Failed : " . $e->getMessage() . "\n";
exit;
}
$query = $pdo->prepare("select field_name from table");
$query->execute();
for($i=0; $row = $query->fetch(); $i++){
echo iconv("CP1256","UTF-8", $row['field_name'])."<br>";
}
3- replace these Items with yours:
IP-database-username-password-field_name-table
4- sometimes you need use "SQL SERVER" instead of "SQL Server Native Client 10.0" and sometimes you need use "IP,port" instead of "IP" and sometimes you need use "ISO-8859-6" instead of "CP1256".
From the PHP manual: http://php.net/manual/en/pdo.construct.php
Example #1 Create a PDO instance via driver invocation
<?php
/* Connect to a SQL Server database using driver invocation */
$dsn = "sqlsrv:Server=12345abcde.database.windows.net;Database=testdb", "UserName#12345abcde", "Password";
try {
$dbh = new PDO($dsn);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Just change the HOST ip to the IP and port of your mysql server.

mySql database connecting to wrong server

I have been trying to setup a Sql database, but I can't get it to connect in php. here's what my code looks like:
$conn = mysql_connect("my_sql_here.net","root",'my_password_here');
print $conn;
mysql_select_db("my_database",$conn);
$created = mysql_query("SELECT * FROM `inventory`);
if(!$created) {
print "error: ";
print mysql_error();
}
print json_encode($created);
mysql_close($conn);
When I run this code, I get:
error: Access denied for user 'dom710'#'localhost' (using password: NO)false
Why is it tryng to connect to localhost? and why is trying to use root as the password?
I am super confused.
Consider using PDO to make a connection:
// Establish a connection
$host = 'my_sql_here.net';
$name = 'my_database';
$user = 'root';
$pass = 'my_password_here';
$dsn = "mysql:dbname=$name;host=$host;charset=utf8";
$conn = new PDO($dsn, $user, $pass);
// Perform your query
$query = 'SELECT * FROM `inventory`';
$statement = $conn->prepare($query);
$statement->execute();
$resultSet = $statement->fetchAll();
// Do stuff with your $resultSet
You have configured safe mode. This is why it tries to connect to localhost.
https://dev.mysql.com/doc/apis-php/en/apis-php-function.mysql-connect.html
$server "In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used."
$username "In SQL safe mode, this parameter is ignored and the name of the user that owns the server process is used."
And as someone stated in comments you shouldn't use this function because it's deprecated.

Can't connect mssql with PHP under mac Yosemite

I'm using PHP 5.5 under Mac Yosemite, the default php with this SO. i'm trying to connect to MSSQL DB server but it's imposible with a lot of alternatives.
I tried to install freetds and the command works but when i tried with PHP...its look he is trying to load but the connection close. My code on PHP is like this:
$server = 'XXX.XXX.XXX.XXX' ;
$user = "username";
$pass = "password";
$DB = "";
$link = mssql_connect($server, $user, $pass) ;
if(!$link){
die('Something goes wrong');
}
I look into php info and it's enabled:
php info
¿Someone knows what is the best alternative to connect to mssql db and works?
Use mssql_get_last_message() to find out what the error is, then, fix the problem.
$server = 'XXX.XXX.XXX.XXX' ;
$user = "username";
$pass = "password";
$DB = "";
$link = mssql_connect($server, $user, $pass) ;
echo mssql_get_last_message();
echo mssql_min_error_severity();
die();
Right now it's working with these lines:
try {
$hostname = 'XXX.XXX.XXX.XXX';
$port = 1433;
$dbname = "YOUR_DB";
$username = "YOUR_USERNAME";
$pw = "YOUR_PASS";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
First you have to install PDO_DBLIB in your system.

Connecting to a remote mysql server

How would I connect to the demo phpmyadmin server in php? My code looks like this.
<?php
$host = 'http://demo.phpmyadmin.net/STABLE/';
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
but I get this as my error
QLSTATE[HY000] [2005] Unknown MySQL server host 'http://www.demo.phpmyadmin.net/STABLE/' (1)
You seem to be confusing two things:
the demo phpMyAdmin front-end that is backed by a db server and db/schema
the db server and schema itself
PDO needs the latter, the db server itself.
Inspecting the front-end code of the demo, I don't see anything in there that would give us the actual connection details for the db server. And that's as I would expect: I find it hard to believe that the makers/maintainers of the phpMyAdmin demo would make their actual db server available for public remote connections.
change your hostname from
$host = 'http://demo.phpmyadmin.net/STABLE/';
to your original remote hostname like eg $host = 'ukld.db.5510597.hostedresource.com';
MySQL does not work on HTTP
<?php
$host = 'demo.phpmyadmin.net';
// High chances that this is NOT your mysql hostname.
// It will not even by like /STABLE/ as you mentioned it.
$dbname = 'shubham';
$user = 'root';
$pass = '';
// Attempt to connect to database.
try {
$DBH = new PDO("mysql:host={$host};dbname={$dbname}", $user, $pass);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>

Categories