I've installed Apache2 with php5 support and everything works there. I've installed PostgreSQL and am able to connect to it using the terminal and execute SQL statements.
PROBLEM: I can't get a working connection between my php scripts and the PostgreSQL database. I have installed the php5-pgsql packaged from the repositories, but the connection just won't work.
I get the following error message:
PHP Fatal Error: Call to undefined function pg_connect() in /var/www/[myfile].php on line [X]
How do I enable support for PostgreSQL connections in PHP5 in ubuntu 11.04?
EDIT: Checked phpinfo() and found no entries for PostgreSQL. I don't know why that is so, I DID install php5-pgsql package for ubuntu 11.04.
Here is what worked: I installed phppgadmin from the Ubuntu repositories. Not only does this make a nice tool available for me now, it also installed the needed packages for php to connect to postgresql.
After that, it was all in the connection parameters. It wouldn't connect to the database on the local server until I defined the connection host, port, database, user, and password in that order in pg_connect().
I still don't know why installing php5-pgsql on my own didn't enable PostgreSQL connections from php. Any input on this would be helpful.
Install the php5-pgsql package solves the problem. (depending on the version ... php4-pgsql for php4)
apt-get install php5-pgsql
Remember to restart Apache.
/etc/init.d/apache2 restart
--Note that it might be hard if you do not administer your server.
Currently, I am using Ubuntu 16.04 LTS.
Me too was facing same problem while Fetching the Postgress Database values using Php so i resolved it by using the below commands.
Mine PHP version is 7.0, so i tried the below command.
apt-get install php-pgsql
Remember to restart Apache.
/etc/init.d/apache2 restart
Below is my code , might be someone get benefited :
- testdb.php
<html>
<body>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
Friend ID
</td>
<td>
Name
</td>
</tr>
<?php
$db = pg_connect('host=localhost dbname=postgres user=postgres password=root port=5432');
$query = "SELECT * FROM account"; //account is name of table
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td></tr>", $myrow['id'], htmlspecialchars($myrow['name']));
}
?>
</table>
</body>
</html>
The only conclusion I can come up with is that phppgadmin installed all of the needed packages to make PHP5 connect to PostgreSQL. I have looked at the dependencies, and I believe that I either didn't install them at all or didn't install them correctly.
I require no more help in this arena, as I have a working setup and know at least one method of getting to that point.
Related
Today some of my packages in Ubuntu was upgraded automatically, and I didn't think of what was actually going on.
Ever since the update, my local dev-environment doesn't work anymore. First of by not working was mod_rewrite which I had to enable again using a2enmod. But now I've run into an issue that I can't seem to resolve. My application can't seem to find the PDO MySQL driver. When running the application, I get the error failed to open the DB connection: could not find driver.
This is strange, since if I check the phpinfo() the PDO drivers do support MySQL, and the socket path is a valid path.
pdo_mysql client API version is 5.5.35 according to php info.
PHP5: 5.5.3
MySQL: 5.5.35
Connectionstring
mysql:host=127.0.0.1;dbname=MyDB;port=3306
What could be causing this?
The PHP MySQL driver (mysql.so/mysqli.so) and the PHP PDO MySQL driver (pdo_mysql.so) are two separate modules. You need both of them for PDO functionality with MySQL.
It is quite possible that one of them is missing or of an incompatible version - I do not have an Ubuntu system at hand, but on my RPM-based Linux distribution there is a separate package for each module (php-mysql/php-mysqli and php-pdo_mysql). I also expect PDO to be using the newer mysqli.so driver, rather than the obsolete mysql.so one, so you should verify that one is installed as well.
Try this:
sudo apt-get install -y php5-mysql php5 mysql-client
This should automatically restart your apache if any of the dependencies aren't installed.
Try using vagrant.
Dependencies can be isolated, upgraded and downgraded when you like.
Vagrant
I have been attempting to connect to an Oracle DB with PHP for some time now with no success. All the guides I have been looking at mention two methods. One is to compile PHP with the oci8 support, which I can't figure out how to do since I installed PHP using yum. The other method is to use the ODBC connection but that also is not working correctly.
The server that I am using for this is an Oracle Enterprise Linux box. I have installed PHP and Apache using yum. I installed php-odbc through yum as well. I have installed the Oracle Client using the Oracle Universal Installer, setup the tnsnames.ora file and tnsping works to the database.
I have been using these as guides for oci8:
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/oow10/php_db/php_db.htm
http://php.net/manual/en/oci8.installation.php
Using PHP 5.3.3 and installed the full client for Oracle 11gR2.
Here is the particular code that I have attempted.
<?php
$connect = oci_connect("username","password","//databaseserver:1521/SID");
if (!$connect){
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
//Close the connection
oci_close($connect);
?>
The error that I am getting when I do a php -F is that it doesn't recognize the command 'oci_connect'. Hence my initial questioning about how to add this support after installing PHP. Things I've been reading have stated that it is included above 5.3. but that is obviously not the case.
Figured this out after much research. If PHP is installed with yum, you can't recompile it but instead must install the individual modules. I was missing several modules that were not indicated by Oracle. This was the installed packages that I finally got this to work with.
php.x86_64
php-cli.x86_64
php-common.x86_64
php-devel.x86_64
php-pdo.x86_64
php-pear.noarch
php-pecl-apc.x86_64
After installing these packages through yum and following the steps in this guide, I was able to get this successfully done.
What is the best way to connect via PHP on a Linux box to a Remote Microsoft SQL Server.
The PHP will only ever run on a Linux box.
I've been trawling for the simplest answer for a while now.
Php 5.6
Ubuntu
sudo apt-get install php5.6-sybase freetds-common libsybdb5
AWS / Centos / Redhat
sudo yum install php56-mssql
After that, you can connect to the MsSql database through PHP with something like this:
<?php
$server = 'localhost';
$user = 'someUser';
$pass = 'somePassword';
$database = 'theDatabaseName';
try {
$pdo = new \PDO(
sprintf(
"dblib:host=%s;dbname=%s",
$server,
$database
),
$user,
$pass
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "There was a problem connecting. " . $e->getMessage();
}
$query = "SELECT * FROM TestSchema.Employees";
$statement = $pdo->prepare($query);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
var_dump($results);
You can troubleshoot MsSQL with something like this on the command line:
tsql -H your.server.name -p 1433 -U yourusername -P yourpassword -D yourdatabasename
To install tsql you can run this:
Installation of SQL binaries for testing on any PHP version
sudo apt install freetds-bin
Php 7+
Microsoft has native drivers we can use. Full instructions are here (Redhat / Ubuntu / Others).
That link also contains required steps to install Ms SQL server on your dev machine (working on Ubuntu, doesn't work on AWS, but you can just spin up an RDS instance there).
It also contains basic instructions on how to create test tables and data in the database, and PHP connectivity code.
You can also install the components for a newer version of PHP like this:
sudo apt-get install php7.2-sybase freetds-common libsybdb5
you must to install mssql driver for php on linux.
this is a best tutorial for you.
Try: For Ubuntu
sudo apt-get install php5-sybase php5-odbc freetds-common
Edit freetds.conf then connect MSSQL server with this PHP.
After searching and trying out the many suggestions here and in other posts, and spending a lot of time, a colleague suggested trying ADO.
As we already had an ADO enabled PHP install, it literally took less than 10 minutes to get up and running.
If your serious about connecting from Linux PHP to MS-SQL, consider ADO.
It's interesting to note that every answer here proposes one solution to the problem rather than answering the question asked which was which is the "best" solution. Unfortunately the OP forgot to tell us what the criteria were for "best".
Nobody so far has mentioned odbc. While this entails both configuring the odbc driver for connecting to the database and enabling the php extension, this method provides the best isolation of the php code from the underlying database making it easier to port the code later.
The microsoft provided drivers for php should give good compatibility but only support v7 up of php.
You may prefer to stick with the functionality provided by your Linux distro to ensure you have a supportable product and/or automated patch management. An alternative to odbc here may be the freetds driver but you didn't tell us anything about which Linux distro this is.
Get the sqlsrv extension from microsoft for php http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
Does the MySQL-server and PHP5-MySQLi version have to match in order for a connection to be possible? I'm currently receiving the error below: I am running BSD.
"Fatal error: Uncaught exception 'PDOException' with message 'could not find driver'..."
Here is the the connection info:
$info = "mysql:dbname=myDB;host=localhost";
$user = "dbUser";
$pw = "somePW";
return(new PDO($info, $user, $pw));
Here is my MySQL information:
mysql-server-5.5.24
php5-mysqli-5.4.3
I had this same issue on my CentOS install. I had tried to install imagick and hosed my install. When I removed all of my php files and reinstalled something wasn't working right.
I ran:
yum install php-pdo
yum install php-pdo_mysql
After doing those two lines I ran
service httpd restart
and everything came back up and running.
PDO uses database specific drivers to connect to database systems. It looks like you are missing the pdo_mysql driver that is required to connect to a MySQL database. There is some details on installing the driver on the pdo_mysql manual page, or there may be a BSD package that you can use (I am afraid I'm not familiar enough with BSD to offer specific advice).
Thanks to zerkms and John C for pointing me in the right direction. Below are the commands I used to install the driver:
#cd /usr/ports/databases/php5-pdo_mysql
#make install clean
#apachectl restart
I installed unixODBC by using apt-get install, and now when I try to use odbc_connect() is still get this error.
PHP Fatal error: Call to undefined function odbc_connect()
what do i need to do to configure it to work with php? I have been looking online but I can't really figure it out.
I think you need to install php5-odbc also. unixODBC provides the driver manager, but you need the PHP code that calls it.
Had the same issue on CentOS 6.3 with PHP 5.3.16. But the fix was to use yum to install php-odbc.
yum install php-odbc
Did you add it to your LD_LIBRARY_PATH? Check the documentation for your server, it may require third-party libraries to be in a specific sub-directory, or have some other mechanism for finding them. If so, you should be able to create a symbolic link to the library. That way, if it gets updated, your server will automatically use it.
We had this problem also. We installed php5-odbc, and still had the problem. Turns out we needed to reboot Linux for php to see the function. Recycling Apache was not enough!
Make sure to enable odbc extension for your Apache by a2enmod odbc.
Then check if exists by: apache2ctl -M.
If you don't have this extension, install via apt-get install php-odbc (use yum in case of CentOS).
See also: Installing the Microsoft ODBC Driver for SQL Server on Linux and macOS.