I have PHP installed on a web server and also have MySQL on the same server and all works perfectly.
I now have SQL Server 2005 installed on another server 192.168.90.250 that I want to connect to as there is a view i want to display in PHP.
I have read around for a number of days but still can not get a connection to work.
I am running on the web server:
Windows Server 2008 R2
Apache 2.2
P 5.3.0
And SQL Server 2005 on another server
I presume I need to download a driver like SQLSRV30 and place the correct .dll file into the C:\Program Files (x86)\PHP\ext directory.
I presume I then need to edit the php.ini file and add something like the following:
extension=php_sqlsrv_53_ts.dll
I would then need to restart the apache server.
At this point I presume I would be able to use something like the following to connect to SQL Server:
<?php
if (function_exists('mssql_connect')) {
die("Your PHP installation does not have MSSQL support.");
}
$db = mssql_connect ($hostname, $username, $password);
if($db){
echo "no connection";
}else{
echo "connected";
}
?>
It seems as if I can not run the function mssql_connect, as the page just displays a blank page.
If I comment out the:
//$db = mssql_connect ($hostname, $username, $password);
the page at least displays some data.
I have literally spent days on this, what am I doing wrong?
Am I using the correct driver as there are different versions?
Thanks for your help.
D
For using the sqlsrv extension you need to use sqlsrv_ functions or better yet switch to PDO
Related
I've been trying to connect to my database, which has been created with PostgreSQL. However, it's unsuccessful.
I've watched lots of videos and researched quite a bit on here, but my issue seems too simple to be an issue for others.
The setup:
I'm hosting a local MAMP (free version) server, in order to use PHP.
I've downloaded and created a database using PostgreSQL locally, and am doing it from pgAdmin 4.
The code is written in a .php file, surrounded by PHP clamps, and the first print line is visible, so I believe there is a connection to the web page in my chrome browser.
I don't get any response from my if-else statement or the pg_query. All the page shows is: This is it:
Issue in short: What is the cause of me not being able to connect to the database and get data from it?
print "This is it:";
$db_connection = pg_connect("host=localhost dbname=first_creation user=postgres password=1234");
if($db_connection)
{
echo "connected";
}
else {
echo "not working";
}
$result = pg_query($db_connection, "SELECT text_content FROM strings");
you need to instll and configure the driver of pgsql in php.ini, please check the following question: How do I enable php to work with postgresql?
Good day,
I am trying to connect to a Microsoft sql server database with the use of php
I downloaded the php driver for sql server and placed them in the extension folder.
I made some adjustment to the php.ini file as well, to include the new extensions.
as the webserver, I am using a server that is similar to XAMPP but instead, this server is a portable one where by you can place it on a usb stick and use other machines to do your developmental work. It is called USB WebServer.
Every time I run the php script, i keep getting the error that says: "could not find driver." I know that this error is an indication that my connection to the database is not successful. I don't know what I have done wrong and I m in need of some help.
The following is my code to connect to the database:
$DB_HOST = 'DESKTOP-AKQUS0J\SQLEXPRESS';
$DB_USER = '';
$DB_PASS = '';
$DB_NAME = 'slddweb';
try{
$pdo = new PDO("sqlsrv:Server={$DB_HOST};Database={$DB_NAME}","
{$DB_USER}","{$DB_PASS}");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage();
}
the above script is stored in a file by the name of connect.php
And I used the following code to detect which version of php I am currently using:
echo 'Current PHP version: ' . phpversion();
which stated that i am using php version 5.4
I downloaded the drivers for php to work with sql server from here:
https://www.microsoft.com/en-us/download/details.aspx?id=20098
I downloaded the third file upon clicking the third check box.
Two files by the names of:
php_sqlsrv_54_ts.dll AND php_pdo_sqlsrv_54_ts.dll
were copied to the extension folder which is located into php folder.
these file names were placed into the extension section of the php.ini file.
After i did the above, I restarted the server ...when i run the php script, i get the error message from the connect.php
any help would be appreciated.
Make sure sql server extenison is loaded in PHP. u can use php -m to check it.
the extension named php_sqlsrv.
And
SQLSRV40 for PHP 7.0+ (Windows、Linux)
SQLSRV32 for PHP 5.6、5.5、5.4(Windows)
SQLSRV31 for PHP 5.5(Windows)
SQLSRV30 for PHP 5.4(Windows)
I am using a linux build server (http://circleci.com) for auto builds and testing; however i need to connect to a MSSQL server. I can develop on windows fine and connect using the MS php SQLSVR drivers, but i cannot connect on the Linux build server as the drivers are windows only.
My Question is this: is there a single way to connect to MSSQL server via php for both windows and linux? tried odbc but then you need bdlib and FreeTDS on linux - this would constitute a code change thus meaning the tests are not 100%, for example on windows environment it would use odbc:{SQL Server} and on linux it would be odbc:FreeTDS
It just doesn't seem right to have a check to see what system is being used and then choose the corresponding db connection string.
I am using PHP 5.5.1
No, there is not a single way however it would be trivial to do something like this:
if ('Linux' === PHP_OS){
$pdo = new PDO("dblib:dbname=$database_name;host=$database_server", $username, $password);
}else{
// $pdo = whatever you're using on your Windows box now.
}
You will need to set up your odbc.ini, odbcinst.ini and freetds.conf files on the Linux server as well.
I'm just getting started trying to set up a server/website and have run into what is probably a very basic issue. I'm getting a blank screen when I try to connect to my installed mysql server with a php script; e.g.
<html>
<body>
<script src="http://protovis-js.googlecode.com/svn/trunk/protovis-r3.1.js" type="text/javascript"></script>
<?php
echo "Connecting..."
$user="root";
$password="passwd";
$server="127.0.0.1";
mysql_connect($server,$user,$password);
echo "Connected.";
?>
</body></html>
shows "Connecting..." but never reaches "Connected". There's nothing wrong with the code or database, since this did work within the browser in Aptana 2.05 (since uninstalled). It doesn't work within Firefox or Chrome. Aptana was using a different (older) version of PHP, 5.2; phpinfo() accessed from Chrome/Firefox was 5.3. The php.ini file contains extension=mysql.so; otherwise I couldn't tell if the settings were correct or not. The tutorials I looked at didn't really cover this sort of thing... anybody know what to do?
What browser you're using has nothing to do with whether the php script running on the server connects to a database or not.
I would suggest checking the server logs, or adding some code to actually check and see what's going on:
$link = mysql_connect($server,$user,$password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
Your MySQL instance may be configured to not respond to localhost. There are two ways of connecting to a MySQL server; through a network connection (ip and port) or socket connection, which only works on the machine it's working on. Your MySQL server may be configured to listen to socket connections but not network.
Try connecting using the mysql command line tool as "mysql -h 127.0.0.1 -uroot -ppassword mysql"
Try running your PHP script from the command line instead of inside the browser.
Ha! Just didn't have the php5-mysql package installed. So evidently having php and mysql by themselves is not enough. Installed php5-mysql, the connection worked.
I have MySQL running such that I can open a client command line and log on and make databases, tables, etc.
I wanted to do some AJAX. Doing AJAX with ASP, SQL Server, etc is not advisable since both places where I am hosting my websites do not use the Microsoft products. So I am forced to do my development with PHP and MySQL.
I just about have everything set up. I have set up IIS so that I can go to my localhost and I can test out web pages. I have PHP installed so that I can pull up the PHP setting pages.
The problem occurs when I try to bring up the MySQL database in PHP. I use this very simple PHP command:
<?php
$con = mysql_connect('localhost', 'testuser', 'testpassword');
?>
When I try to connect to the mysql database through PHP, I get this error:
Click here
I figure the problem must be in the settings that are in the php.ini. But it seems that my php.ini is set up for MySQL although it is not mentioned in the output when I query the phpinfo page with this code
Here is the result from this:
Click here
It looks that your php is missing mysql module
in php.ini make sure that you have correct extensions path eg.:
extension_dir = "C:\php\ext"
and make sure you have commented out:
extension=php_mysql.dll
extension=php_mysqli.dll
Which version of PHP are you running and are you sure you have MySQL installed and running on localhost with a username of "testuser" and a password of "testpassword" (and have you reloaded the privilege tables after creating those users)?
Have you got IIS configured to log errors into a file - does that provide any more information?
Create a new PHP file that contains the following:
<?php
phpinfo();
?>
This will helpy you to discover if MySQL has been compiled in properly etc. Can you access the MySQL server from the command line; using something similar to:
mysql -u testuser -ptestpassword testdatabase
If you get a prompt from MySQL, the service is running and we can better help from there.
Hope that helps a little!
Are you selecting the database? After your connect statement, you need to use:
mysql_select_db($databaseName, $conn);
Documentation here: http://php.net/mysql_select_db
What I would do is to download the complete package in one go (Server, MySQL and PHP). There are tons of good resources like WAMP or MAMP (for MAC users). Once you download the package the installation is just easy and you don't have to set anything as it does it automatically.
They also have phpMyAdmin where you can manage your database and its users and privileges.
Once you got your server running you can test that code that seems fine for me. Try running something like this:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected';
mysql_close($link);
?>
Cheers