I'm new to PHP. PHP is installed and working, and now I'm trying to set up connection with a SQL Server DB. Following instructions from a Youtube video. Went through and finished installing the drivers, but it isn't working. My test page stops running code after the sqlsrv_connect() function is called. I'm not even getting an error message. Below is my test page code:
<?php
echo '<br><span style="font-weight:bold;">PHP functioning. This line is being echoed out.</span><br />';
/*No problems yet...*/
$serverName = "Myserver";
$connectionInfo = array('Database'=>'nde', 'UID'=>'sa', 'PWD'=>'!hegEcu&3ATr');
$conn = sqlsrv_connect($serverName, $connectionInfo);
/*Nothing is appearing on the screen after this */
echo 'Still working...<br />';
if ($conn) {
echo 'Connection established.<br />';
} else {
echo 'Nope.<br />';
die(print_r(sqlserv_errors(), TRUE));
}
?>
I'm running PHP 7.0.21, and SQL Server 2014. I went and downloaded the two DLL files I think I need - php_pdo_sqlsrv_7_ts_x64.dll, and php_sqlsrv_7_ts_x64.dll. I copied them to Program Files/PHP/v7.0/ext. I went into PHP.ini as admin, and typed:
extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_sqlsrv_7_ts_x64.dll
in the Windows Extensions sections. I saved PHP.ini, restarted both the IIS Admin Service and World Wide Web Publishing Service, and went into IIS Manager and restarted the server. PHP is still working, but I'm still not getting any echo after the sqlsrv_connect() call.
Okay, I got it working. As ctwheels suggested, i just installed the nts driver versions instead of the ts versions, which are php_pdo_sqlsrv_7_nts_x64.dll and
extension=php_sqlsrv_7_nts_x64.dll. It's working with sqlsrv_connect(); I haven't tried using PDO. I'm new to PHP and unfamiliar with PDO, but I'll probably come across it again, especially as I start looking into PHP frameworks (Cake and CodeIgniter).
One important thing to share for anyone who might also be new to PHP - the PHP documentation says that the $serverName argument of sqlsrv_connect() should be in the format of servername/instancename. So I used this query to get both: select ##servername + '\' + ##servicename, and got "Myserver\MSSQLServer". However, using this as the $serverName argument caused the connection to fail. Only after I took out "\MSSQLServer" did it connect successfully. So as in the code above, the correct value to use was $serverName = "Myserver".
Related
I've been trying to connect to my Azure SQL database using PHP 7.1. I've been following Microsoft's instructions so I'm using the iis express. After browsing and following the directions from posts like "Call to undefined function sqlsrv_connect()" when trying to connect to Azure DB from PHP and
Cannot Call function sqlsrv_connect(), I still cannot connect.
<?php
$serverName = "database_name.database.windows.net";
$connectionOptions = array("Database" => "database_name");
try {
$conn = sqlsrv_connect($serverName, $connectionOptions);
} catch (Exception $e) {
print_r($e);
}
?>
Here are the solutions I've tried:
Installed all the necessary SQL drivers
Reconfigured the php.ini file with the extensions and correct extension path
Ran PHP as an administrator from the PHP folder and also from another folder using its %PATH%
Restarted several times
Connected successfully to the database from the CMD line
This is my first time using PHP to connect to a remote SQL server so any help would be greatly appreciated.
You need to check whether your drivers match the PHP Version, Architecture (32/64 bits) and Thread Safety you are using.
For example, if you are using x86 and thread-safe version of PHP 7 then the following DLLs should be used:
extension=php_sqlsrv_7_nts_x86.dll
extension=php_pdo_sqlsrv_7_nts_x86.dll
You can see these PHP info via phpinfo() function:
I've spent many-a hour trying to get a local webserver working (I'm new)
I created a sqlite3 database ('database.sql') in the www folder, and tried calling it with numerous different php commands (php 5.5, I checked) such as '$test = new SQLite('database.sql')' or the same with SQLite3, both with no luck. Also tried $test->open('database.sql').
Always with the fatal error "Class 'SQLite' not found". I've spent too many hours on what I'm sure is a very simple problem, I'm sorry to have to ask this!
To open a DB using PHP5 and SQLite we need to use a PDO and not the sqlite_open() function.
This is how to open or create a database: (not sure if it's bug free)
try {
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:VPN0.sqlite");
echo "Handle has been created ...... <br>";
}
catch(PDOException $e) {
echo $e->getMessage();
echo "<br>Database is loaded UNSUCCESSFULLY .. ";
die("<br>Query is closed $error");
}
echo "Database loaded SUCCESSFULLY ....";
Hope this helps!
There is a php package called sqlite3 which you can use (as well as PDO which is given above). Here is a fraction of code which uses it.
$db = new SQLite3(DATABASE);
if (isset($dbversion)) { //only newer versions of chat will have this
$version = $db->querySingle("SELECT value FROM parameters WHERE name = 'db_version'");
Where the DATABASE variable has been defined with
define('DATA_DIR',$datadir); //Should be outside of web space
define('DATABASE',DATA_DIR.'chat.db');
Are you sure that the package is installed on your server?
If you're still having problems you can do a couple of things. First, use phpinfo(); in a page to determine if the SQLite3 extension is installed.
Second, if you want to use PDO make sure that the following line in your php.ini is un-commented:
extension=php_pdo_sqlite.dll
If you have to un-comment this you will need to restart your server for the changes to take effect.
this is something I've really been losing sleep over, so I'd be very thankful if anyone could help me.
So, I'm trying to set up a launcher for a game. I have a Login.php on my web server and the launcher in Visual Studio. When pressing "Start" it connects to Login.php where it checks if the game login credentials are correct, if they are, it should return a string of random letters (a token) to the Launcher. The Login.php tries to connect to my database in SQL Server 2008.
Now to the problem, I get
Fatal error: Call to undefined function mssql_connect() in C:\inetpub\wwwroot\Login.php on line 12
when trying to go to the website manually (By entering 127.0.0.1/Login.php into the browser.). And when I try to run the program and press start, it returns a 500 Internal Error.
I have googled the mssql error and found out that the mssql functions have been removed from the newer versions of PHP, so I downloaded the drivers from Microsoft and enabled them in the PHP Manager on IIS 7.5.
The version of PHP I use is 5.4.25 and I enabled these drivers:
php_pdo_sqlsrv_54_nts.dll
php_sqlsrv_54_nts.dll
And here are the first 13 lines of the Login.php:
<?php
$conf['db_host'] = "GERA044\SQLEXPRESS";
$conf['db_user'] = "sa";
$conf['db_pass'] = "L4sfa445AdxDDLfor95";
$conf['db_name'] = "Account";
$user = sql_clean($_GET['Username']);
$passhash = sql_clean($_GET['Password']);
$con = mssql_connect($conf['db_host'],$conf['db_user'],$conf['db_pass']) or die('Database connect Fail.');
$db = mssql_select_db($conf['db_name'], $con) or die('Database Init Fail.');
In short: I still get an mssql error even though I have the drivers.
Hopefully someone can find a solution to the problem here,
Thank you!
php_sqlsrv_54_nts.dll is the SQLSRV extension, not the MSSQL one. You should use the sqlsrv_* family of functions instead of the mssql_* one.
I'm running a Linux Fedora 19 setup with Oracle 11g, PHP 5.5.4, Apache 2.4.6, and PEAR 1.9.4. In addition to this, I have the OCI8 plug-in for PHP downloaded and enabled, and have downloaded MDB2 and the MDB2_Driver_oci8.
The goal is to connect my webpage to the Oracle 11g database. This already works in an existing setup (which I didn't do), and I'm trying to replicate it in a new setup.
For some reason, in the new server, it doesn't work. It generates an error:
_doConnect: [Error message: unable to establish a connection]
** oci8 (oci8)://gbsihr:xxx#localhost:1521/
An error occurred while trying to connect to the database server.
Error message: MDB2 Error: connect failed
I truly have no idea what this means. This is my PHP connect function:
<?php
include_once('includes/configure.inc');
require_once 'MDB2.php';
function DB_connect($intSilent = -1){
$db_dsn = $_SESSION['DB_Type'] ."://";
$db_dsn .= $_SESSION['DB_Username'] .":";
$db_dsn .= $_SESSION['DB_UserPassword'] ."#";
$db_dsn .= $_SESSION['DB_Host'] .":";
$db_dsn .= $_SESSION['DBPort'] ."/?service=";
$db_dsn .= $_SESSION['DB_DbName'];
if (PEAR::isError($dbconnect)) {
print '<pre>';var_dump($intSilent);print '</pre>';
print '<pre>';var_dump($db_dsn);print '</pre>';
print '<pre>';print $dbconnect->getDebugInfo();print '</pre>';
echo "An error occurred while trying to connect to the database server.<br>\n";
echo "Error message: " . $dbconnect->getMessage() . "<br>\n";
echo "A more detailed error description: " . $dbconnect->getDebugInfo() . "<br>\n";
exit();
// Check whether the object is a connection or
if($_SESSION["gENDebugInfo"] == "On"){
$_SESSION["gErrorNote"] = $dbconnect->getDebugInfo();
}
if ($intSilent < 0) {
header("Location:" . $_SESSION["gENWebRoot"] . "showerror.php?ec=1003"); // Failed to connect
exit();
}
}else{
$dbconnect->setOption('portability', MDB2_PORTABILITY_ALL);
}
return $dbconnect;
}
?>
And this is the contents of configure.inc:
$_SESSION["gENWebRoot"] = '/hrweb/';
$_SESSION['DB_Type'] = 'oci8';
$_SESSION['DB_Host'] = 'localhost';
$_SESSION['DB_Username'] = 'gbsihr';
$_SESSION['DB_UserPassword'] = 'ids21';
$_SESSION["DBPort"] = '1521';
$_SESSION['DB_DbName'] = 'xe';
As this works on another setup, I'm not sure where the error lies. OCI8 seems to be in order, though I did go through hoops to get it working.
I did do some research, with only two vague results - one pointing to the oci8.php itself, and the other, an Oracle TNS error (unlikely, given the lack of errors).
Here are my attempts to solve the problem:
I tried copying the Basic Query Example from this website. I modified the "hr" and "welcome", and ran it on my new setup (it works fine on the older one). And this is the result:
oci_connect(): OCIEnvNlsCreate() failed. There is something wrong with
your system - please check that ORACLE_HOME and LD_LIBRARY_PATH are
set and point to the right directories. Error while trying to
retrieve text for error ORA01804
I've also tried opening the httpd file in /etc/sysconfig/ and adding the following line to it:
LD_LIBRARY_PATH="/u01/apps/oracle/product/11.2.0/xe/lib
export LD_LIBRARY_PATH
And yet when I try to echo $LD_LIBRARY_PATH in the CLI, it won't return anything. It does appear in phpinfo(), though:
This is in contrast to $ORACLE_HOME, which I have to load manually every start-up by typing in . /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh in the CLI, and does not appear under environments, but at least, can be echo'd.
There are a lot of variables in the old setup under environment; LD_LIBRARY_PATH, ORACLE_HOME, and ORACLE_SID being the most noteworthy. I only see LD_LIBRARY_PATH in the envvars file, though. Should I add them as well to the httpd file?
I'm not much of a Linux-person, sorry. Any help on this would be much appreciated.
I suggest you try to connect to your oracle database by using the plain oci8 functions and the same login parameters as you use for MDB2.
My guess is that this will fail as well, but might give you more error details.
Can you connect to oracle outside of php?
I presume you have a valid tns.ora file set up.
I needed these two lines set in my VirtualHost definition, note TNS_ADMIN is defined also, perhaps you do too:
SetEnv ORACLE_HOME /opt/oracle/instantclient
SetEnv TNS_ADMIN /usr/lib/oracle/11.2/client/network/admin/network/admin**
Just add this line
$dbconnect = DB::connect($db_dsn);
to function DB_connect
like this
...
$dbconnect = DB::connect($db_dsn);
if (PEAR::isError($dbconnect)) { ...
I'm having heavy issues trying to connect to my MSSQL 2012 Express database, with PHP5 running on an Apache.
I have as a test setup just installed a XAMPP with PHP 5.4.4 and Apache running on a Windows 7 machine.
My PHP code (phpmssql_genxml.php):
$connection = mssql_connect('192.168.40.150', $username, $password);
if (!$connection) { die('Not connected : ' . mssql_get_last_message());}
$db_selected = mssql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mssql_get_last_message());
}
$query = "SELECT * FROM Cust WHERE 1";
$result = mssql_query($query);
if (!$result) {
die('Invalid query: ' . mssql_get_last_message());
}
Output when trying to enter the site:
Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\phpmssql_genxml.php on line 13
Even if I try to hardcode the username and password into the string, I still get the same result.
Have search a lot on google, but havn't found that post that fixed my issue yet :/
Have enable TCP/IP for the DB instance pipe, even try'd to assign a specific TCP port for it. Have created a rule in the Win7 firewall allowing all traffic to the standard port 1433. Still no luck.
any1 have an idea?? What does the 'Fatal error' part means? Is it the Apache error, PHP or a Database error when trying to connect to it??
You are missing MSSQL driver from your PHP setup. Download it from here, assuming you have the required system configuration mentioned on the page.
Setting up, from their instructions:
Download SQLSRV30.EXE to a temporary directory
Run SQLSRV30.EXE
When prompted, enter the path to the PHP extensions directory
After extracting the files, read the Installation section of the SQLSRV30_Readme.htm file for next steps
I would also recommend using standard Apache + PHP installation, if you plan to work with MSSQL, instead of any *AMP package.
You don't need to use SQLSRV30 as this was not the solution that I needed though some may find it useful. I have a local dev environment running XAMPP + php5.5.9 + MSSQL2012 and I needed to use mssql functions because thats what we use at work on our servers. So I use freetds and "luckily" found this:
https://moodle.org/mod/forum/discuss.php?d=232844
where one of the users already compiled the php_dblib.dll TS and NTS for my php version suffice to say I have everything working and using all the MSSQL functions that our dev team is used to. The extension for mssql support can always be compiled.