We are using the below script.
Actually our DB is in one server (say www.server1.com) and the PHP file
containing the below connection string is in another server (say
www.server2.com)
If we place the PHP file containing the below script in the same server
where the DB exists that is www.server1.com/dbconnection.php it is working
fine.
But if we place the PHP file containing the below script in another server
www.server2.com/dbconnection.php it is not working. This displays the
error 'Something went wrong while connecting to MSSQL' Please advise.
Also to handle error if we use 'die('MSSQL error: ' .
mssql_get_last_message());' nothing displays just an empty page.
Please advise how to handle errors.
Script (dbconnection.php)
$server = 'xxx.xxx.xxx.x,xxxx'; //IP, Port
// Connect to MSSQL
$link = mssql_connect($server, 'username', 'password');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
// Connect to DB
$db=mssql_select_db("databasename",$link) or die("Unable to select
database ");
Thank You
How to get error message, I do not know, but if you are migrating your PHP app from windows server to linux (FTP servers are more usual on linux), you can try change:
$server = 'xxx.xxx.xxx.x,xxxx'; //IP, Port
to
$server = 'xxx.xxx.xxx.x:xxxx'; //IP: Port
As described in first parameter of manual: http://www.php.net/manual/en/function.mssql-connect.php
Related
I am trying to log-in remotely to MySQL server which is running on my Disk station, over the network. I confirm that MySQL is running on the disk station, I have php running on the disk station and I can check via remote connection to disk station phpMyAdmin and MySQL queries are answered.
On my laptop, I have created the following PHP script and when I try to run this script on my laptop, I get the following error message
<?php // login.php
$hn = 'bio12' ; //disk station name
$db = 'mysql' ; //database I want to connect to
$un = 'mysqluser' ; // user name
$pwd = 'abctest123' ; // password for the user
$conn = mysql_connect($hn, $un, $pwd);
if(! $conn){
die('Could not connect') ;
}
echo 'Successfully Connected';
?>
Error message: Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\login.php on line 15
Could not connect
I have tried using the IP address instead of 'bio12' but without success.
Interesting enough, when I use the same code and try to connect to local mysql (I am running XAMP on my laptop) and using the 'localhost' instead of 'bio12' it works just fine.
What am I doing wrong?
You need to allow remote connections. Find line in your mysql config (my.cnf file)
bind-address = 127.0.0.1
and comment it with "#" at the beginning, please also see this thread: How to allow remote connection to mysql
I have application with php. I want to connect on sql server 2000
This is my code:
$server = '10.0.0.26';
// Connect to MSSQL
$link = mssql_connect($server, 'username', 'password');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
But I have error like
Message: mssql_connect(): Unable to connect to server: 10.0.0.8. I
make sure that i use correct username, password and ip.
When I use this code to connect sql server 2012. I did't get any error.
I run my php using apache on linux.
The first parameter should be SERVERNAME not Server's IP as it is mention in the PHP DOCS
eg ( from php manual)
$server = 'KALLESPC\SQLEXPRESS';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
Check your php.ini file:
; Use NT authentication when connecting to the server
mssql.secure_connection = On
If you have secure_connection = On, make sure that you provide valid credentials in the properties for Apache service in the System Services box. Then you should not send DB username and password from your script to MSSQL Server.
If you want to use specific credentials from a PHP script, then set
mssql.secure_connection = Off
in your php script.
I am new to PERL script and I want to collect data from PHP/MySQL in a Linux server, using PERL script.
I have tried some sample PERL code and it works on a local environment.
But I need to get data from another Linux server:
#!/usr/bin/perl
use DBI;
# connect to MySQL...
$driver= "mysql";
$dsn = "DBI:$driver:database=mms;host=localhost";
$dbh = DBI->connect($dsn, "root", "");
# prepare and execute the SQL statement
$sth = $dbh->prepare("SELECT * FROM dailyproductions");
$sth->execute;
# retrieve the results
while( my $ref = $sth->fetchrow_hashref() ) {
print $ref->{'id'};
}
exit;
This code works locally (localhost), but when I switch to a different host:
$dsn = "DBI:$driver:database=mms;host=192.168.0.1";
I get this error:
DBI connect('database=mms;host=192.168.0.1,'root',...) failed: Host 'admin-
lap' is not allowed to connect to this MySQL server at D:\Confidential\Report\mysql.pl line 7
Can't call method "prepare" on an undefined value at D:\Confidential\Report\mysql.pl line 10.
How can I overcome this?
You're not using "phpmysql", you're using MySQL. Try not to get the two confused.
Your program works. You've established that. The problem is with the MySQL connectivity between the machine where you're running the program and the machine with the MySQL database server. That's what your error message is saying.
Host 'admin-lap' is not allowed to connect to this MySQL server
You will need to get the MySQL configuration changed to allow your host to connect to the server. Your database administrator will be able to help with that.
One fix I would make to your program. You see the second error, about calling prepare on and undefined value? That's because your program assumes that it can always connect to the database. You should change that so that the program dies if it can't make the connection.
$dbh = DBI->connect($dsn, "root", "")
or die $DBI::errstr;
This might be a stupid question but why am i using local host if my site is being hosted with rackspace?
$db_host = "localhost";
$db_username = "*****";
$db_pass = "********";
$db_name = "lds";
$link = mysql_connect('localhost','*****','*********');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
It appears to be getting a connection because I'm successfully echoing 'Connected successfully'.
Maybe something is terribly wrong because i noticed when i took out localhost from both mysql_connect function and from the db_host variable... it still says connected successfully.
Would that be because i've already wired the ftp connections up in my IDE?
Any help would be greatly appreciated. Thanks in advance.
Your database server is running locally to the remote machine, where you are executing your PHP script(s). To your script the database appears to be localhost even though the web server and database server are remote relative to you.
Per the documentation for mysql_connect:
If the PHP directive mysql.default_host is undefined (default), then
the default value is 'localhost:3306'.
This would seem to indicate that if no value for the database server is provided, the default is used. Also note that the documentation also states that this function is deprecated in PHP 5.5.0.
See instead: http://php.net/mysqli and http://php.net/pdo
As long as your php we server and the MySQL database server are on the same machine localhost will work
Using ubuntu 12.04 64 bit on Lenovo t410.
Using apache2 and Mysql 5.5 and attempting to connect via localhost
I am attempting to establish a connection to a database that I made on localhost. When the line of code is reached to establish a connection, it seems Mysql simply hangs, and there is no error message displayed after. I verified that an echo works immediately prior to the connection attempt. I know that apache2 server is working as I can access the index page and display my html form.
I have tried etc/mysql/my.cnf setting the bind address to localhost.
My line of code looks like:
// Attempts to establish connection to MySql server
$connection = mysql_connect("localhost","username","password");
// Prints error message if the connection to MySql fails
if (!$connection){
die("Connection failed: " . mysql_error());
}
echo "Connection established.";
I tried the connection line with single quotes and with no semi-colon as well.
I am willing to post the contents of any configuration file I have if the error isn't syntax. I haven't done anything fancy to Ubuntu, everything is the default install. I am new to CS and especially databases, PHP, and networking. This is my little experiment that I am stuck on.
Any help is greatly appreciated. Thanks,
Don
Can it be, because there is no error message, that the connection IS established, but you didn't do anything with it?
I mean, what is the rest of your code, is there after your code here something like:
mysql_select_db("database_name",$connection);
After reading your last comment, it appears the mysql extensions are not being loaded. Have a look at your php.ini, uncomment the following line (remove the semicolon at the beginning of the line) and restart your apache:
extension=php_mysql.so
Make sure the extension exists in the php extensions directory.
Due to the fact that you are using MySQL version > 4.1.3 it is strongly recommended that you use the mysqli extension instead. Have a look at this: PHP: MySQL Overview
try to set
$mysql_user = "your_username";
$mysql_pass = "your_password";
$mysql_server = "Servername";
$link = mysql_connect($mysql_server,$mysql_user,$mysql_pass);
if (!$link) {
header('HTTP/1.1 500');
exit();