PHP Connecting to MSSQL Fatal Error - php

Here is my problem: Fatal error: Call to undefined function mssql_connect().
I installed 2 drivers for this, as explained in other posts.
php_pdo_sqlsrv_56_nts.dll
php_sqlsrv_56_nts.dll
<?php
$myServer = ".";
$myUser = "LoginDisciples";
$myPass = "********";
$myDB = "Disciples";
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
?>
The login information is all correct.
Please help!

You should use PDO
If there is a missing driver - here is an Ubuntu example how to install:
1) download driver sudo apt-get install php5-xxx
2) activate driver sudo php5enmod xxx
3) restart apache sudo service apache2 restart

Related

Howto connect with PHP to a MS SQL Server within a Docker setup?

I am trying to connect to a MS SQL server with PHP in a Docker environment.
I followed this instructions for my Dockerfile:
FROM php:7.3.10-apache
RUN apt-get -y install unixodbc-dev
RUN pecl install sqlsrv pdo_sqlsrv
The following two lines are added to the php.ini:
extension=pdo_sqlsrv.so
extension=sqlsrv.so
The phpinfo() shows this:
Now I am trying to connect to the MS SQL Server (which is not part of the docker environment):
$server = 'MyServer';
$database = 'MyDB';
$username = 'MyUser';
$password = 'MyPassword';
# Connect
try {
$conn = new PDO("sqlsrv:server=$server;Database=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Error connecting to SQL Server: ".$e->getMessage());
}
But I am getting the following error:
Error connecting to SQL Server: SQLSTATE[IMSSP]: This extension
requires the Microsoft ODBC Driver for SQL Server to communicate with
SQL Server. Access the following URL to download the ODBC Driver for
SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712
Can someone point me in the right direction? What exactly do I need to do?

Eclipse: "PHP Fatal error: Call to undefined function mysqli_connect()"

I have a php script (machine_db.php) which makes a connection to mysql database as follows:
<?php
//phpinfo();
// 1 connect to mysql
$servername = "localhost";
$username = "root";
$password = "abcd";
$conn = mysqli_connect($servername, $username, $password);
...
?>
I am using:
Server version: Apache/2.4.12 (Ubuntu)
PHP 5.6.11
mysql Ver 14.14 Distrib 5.6.28
I am using Eclipse Mars and I installed PHP packages (addin or whatever it is called) for Eclipse. Then I created a php project and added my php file to the project and did run as php cli application but I get the following error in Eclipse console,
PHP Fatal error: Call to undefined function mysqli_connect()
However I don't get any error when I run the exact same script from terminal like:
php machine_db.php
I also see that in my Eclipse project explorer there is a PHP Language Library which contains mysqli class.
I think you forget the db name:
$servername = "localhost";
$username = "root";
$password = "abcd";
$db_name = "my-db";
$conn = mysqli_connect($servername, $username, $password, $db_name);
You can also use
$conn = new mysqli($servername, $username, $password, $db_name);
Documentation: http://php.net/manual/fr/mysqli.construct.php
Also try to install php5-mysqli
sudo apt-get update
sudo apt-get install php5-mysql php5-mysqli
sudo service apache2 restart
dont forget to uncomment this line in your /etc/php5/apache2/php.ini
extension=php_mysql.so
extension=php_mysqli.so
Dont forget to restart apache server
sudo service apache2 restart
I just realized that if in eclipse i check "use system default php.ini" under window->preferences->php->phpexecutables it will magically work.
Does anyone know what is "system default php.ini"?
I do a a quick $php --ini and it returns:
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File: /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed: /etc/php5/cli/conf.d/05-opcache.ini,
/etc/php5/cli/conf.d/10-mysqlnd.ini,
/etc/php5/cli/conf.d/10-pdo.ini,
/etc/php5/cli/conf.d/20-json.ini,
/etc/php5/cli/conf.d/20-mysql.ini,
/etc/php5/cli/conf.d/20-mysqli.ini,
/etc/php5/cli/conf.d/20-pdo_mysql.ini,
/etc/php5/cli/conf.d/20-readline.ini

Cannot connect to MYSQL using PHP

I could not connect to the local host MYSQL database using my simple php code and DOES NOT display anyything. THis is applicable to all users and root.Eventhough the problem is reported many times, i could not fix it. Also it is written in one site that it may have to make changes in php file.Please give me solution for this connection error.
I use php 5.5.9-1 in Ubuntu. MYSQL version is 5.5.38.
My code is
<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$conn = mysql_connect($servername, $username, $password);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Call to undefined function mysql_connect()
This error message from your comments tells me that you do not have the MySQL extension installed.
Execute this in your terminal to install: sudo apt-get install php5-mysql
Then restart Apache: sudo /etc/init.d/apache2 restart
Please be aware that the "mysql_" functions are marked as deprecated and your script might fail with a future PHP update. Please use "mysqli_" or PDO.
Mysql is deprecated. Use Mysqli.
Create a file and name it as db_connect.php and copy the below code.
<?php
$connection = mysqli_connect("localhost","username","password","database_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Open browser and type http://localhost/db_connect.php. If you see a blank page, your connection is fine. If there's a problem, it will show you the error.

Apache will not connect to SQL Server with PHP

I'm trying to open a connection to a SQL Server 2008 on a windows computer from an apache linux server via php. I've opened up the appropriate port on the firewall, but I am getting the vaguest error
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: xxx.xxx.xx.xxx,xxxx
with:
$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
$myUser = "un";
$myPass = "pw";
$myDB = "db";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die( mssql_get_last_message());
Is there some way to get a more specific error message? Or some way that I can test to see if the two computers are communicating at all so I can try to begin to localize the problem?
Here's the code I use to connect PHP to MSSQL from Ubuntu machines to Windows SQL Server, I don't know if it will help you or not but this code is up and running successfully right now so I know it works in our environment...
As you can see, I use PDO rather than the mssql_* functions. On Ubuntu I needed to install the php5-sybase package to get the dblib driver.
PHP:
<?php
try{
$con = new PDO("dblib:dbname=$dbname;host=$servername", $username, $password);
}catch(PDOException $e){
echo 'Failed to connect to database: ' . $e->getMessage() . "\n";
exit;
}
?>
/etc/odbc.ini
# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssqldb]
Description = MSSQL Server
Driver = freetds
Database = MyDB
ServerName = mssqldb
TDS_Version = 8.0
/etc/odbcinst.ini
# Define where to find the driver for the Free TDS connections.
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
/etc/freetds/freetds.conf
[global]
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# Define a connection to the MSSQL server.
[mssqldb]
host = mssqldb
port = 1433
tds version = 8.0
I ran into the same problem today. This works for me:
Instead of this
$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
try this:
$myServer = "mssqldb"; //must correspond to [entry] in freetds.conf file
In your case, I'd rename the entry and use the fully qualified hostname to the entry in the config file just for clarity
# Define a connection to the MSSQL server.
[mssqldbAlias]
host = mssqldb.mydomain.com
port = 1433
tds version = 8.0
The first argument in the call to mssql_connect() must correspond to an [entry] in the freetds.conf file.
So your call becomes
$myServer = "mssqldbAlias";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die( mssql_get_last_message());

Want to connect PHP5 to MS SQL SERVER 2005 in linux

I want to connect mssql server using php code on linux operating system. i want to use mssql_connect() .please help me. my code is
$server='x.x.x.x\SQLEXPRESS';
$link = mssql_connect($server, 'username', 'password');
if (!$link)
{
die("Couldn't connect to SQL Server on $server. Error: " . mssql_get_last_message());
}
else
{
echo "Connected!";
}
it show error "Unable to connect to server: x.x.x.x\SQLEXPRESS"
For me, this worked under debian lenny:
apt-get install libsybdb5 freetds-common php5-sybase
/etc/init.d/apache2 restart
I found the solution here: http://docs.moodle.org/20/en/Installing_MSSQL_for_PHP

Categories