php pg_connect() works in terminal but not in the browser - php

<?php
echo "Inside php file!<br>";
$dbconn = pg_connect("host=localhost dbname=atharvacm user=atharvacm port=5432 password=Dominicmcs9") or die("Cannot connect to DB");
echo "after con<br>";
$query = "select * from person;";
$result = pg_query($dbconn, $query) or die("cannot exec query");
while ($answer = pg_fetch_row($result)) {
echo $answer[0];
echo $answer[1];
echo " huh<br>";
}
echo "<br>end";
?>
this is the code im trying to execute. It outputs the records in the table person in terminal but in the browser it only echoes "Inside php file" and nothing after it.
Here is the terminal output:
atharvacm#kali:/var/www/html$ php psqltest.php
Inside php file!<br>after con<br>1abc huh<br>2def huh<br>3pqr huh<br>4xyz huh<br><br>end
atharvacm#kali:/var/www/html$
Here is the output from the web browser
Inside php file!
Fatal error: Uncaught Error: Call to undefined function pg_connect() in /var/www/html/psqltest.php:4 Stack trace: #0 {main} thrown in /var/www/html/psqltest.php on line 4
I have kali linux and running apache2, psql, php7.
I installed the php-pgsql package and uncommented the extention in the php.ini file.
I cant seem to figure out what the problem is but my guess is that the pg_connect function is not finding the psql database or something form the browser.
thanks!

Please check the config.ini file of ur php configuration file "php.ini" within Apache Tomcat
In some case the software is not configured for psql
So go in the file and uncomment the line
Extension= pgsql
Also enable ur error log in Apache
U can refer
Please check the config.ini file of ur php configuration file "php.ini" within Apache Tomcat
In some case the software is not configured for psql
So go in the file and uncomment the line
Extension= pgsql
Also enable ur error log in Apache
U can refer
Please check the config.ini file of ur php configuration file "php.ini" within Apache Tomcat
In some case the software is not configured for psql
So go in the file and uncomment the line
Extension= pgsql
Also enable ur error log in Apache
U can refer
Please check the config.ini file of ur php configuration file "php.ini" within Apache Tomcat
In some case the software is not configured for psql
So go in the file and uncomment the line
Extension= pgsql
Also enable ur error log in Apache
U can refer
How do I get PHP errors to display?

Related

My computer has two php folders so I either can't connect to SQL Server or install Laravel

I spent two days setting up my PHP connection to SQL Server.
I downloaded the drivers, added them to the C:\Program Files\php\ext folder (as indicated by phpinfo()). Then I modified the php.ini file to add those extensions. Then I went to services and restarted "SQL Server EXPRESS". Nothing worked until I found that PHP is also installed in another folder on my PC: C:\Program Files\iis express\PHP\v8.0. So I added the drivers there too and changed the php.ini file and restarted the SQL Server EXPRESS service (is that what they mean with restarting your web server?). The phpinfo() page said:
Configuration File (php.ini) Path: no value
Loaded Configuration File: C:\Program Files\iis express\PHP\v8.0\php.ini
and there was no section "sqlsserver". I gave up and the next day it magically worked. The phpinfo had a "sqlserver" section and the connection to my database no longer said "Call to undefined function sqlsrv_connect()".
Now, I had to download Laravel for my course, and install Composer. When doing so, it was unable to install when I had C:\Program Files\iis express\PHP\ as my PATH and it asked me if I wanted to make C:\Program Files\php\ my PATH instead. So I clicked yes, and the installation was successful.
Laravel works, however now my database connections with SQL Server don't work anymore! I get the same old "Call to undefined function sqlsrv_connect()" error, and my phpinfo() now says
Configuration File (php.ini) Path:
Loaded Configuration File: C:\Program Files\php\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
and there is no longer a "sqlsserver" section on the page.
I have tried everything in StackOverflow articles, such as removing the semicolon in the php.ini extension, including the full address in the extensions in the php.ini file, including the extensions of the other php folder into this php.ini file, restarting SQL Server service, etc. But nothing works. I also don't know how to reset the old PHP folder back into the PATH variable.
My connection code is:
$con = sqlsrv_connect( 'localhost' ,
[ 'Database' => 'sample_db' ,
'UID' => 'sample_user' ,
'PWD' => 'sample_password' ]
);
//Print error message if the database doesn't connect
if ($con === false ){
echo 'Failed to connect to db: ' . sqlsrv_errors()[ 0 ][ 'message' ];
exit ();
}
How can I have these two php folder on my computer both work, the one for my PHP SQL Server database connection, and the other for my Laravel programme to run? Has anyone else had this issue and I so, what worked for you?
I managed to resolve this by changing the PATH back to the php folder instead of the iis/php folder. I was worried that would break my Laravel now, but it didn't.

PHP cURL works locally, missing on Apache server

PHP Version 7.2.1
Apache Version: Apache/2.4.29 (Win64) OpenSSL/1.0.2n PHP/7.2.1
OS: Windows Server 2012 R2
I enabled cURL in my php.ini file.
When I try to load modules in console: php -m
I got:
There we can see that cURL is loaded and it is working when I use php [script path] command
But cURL is not loaded on my Apache Server
i have info.php page which contains:
<?php
if(in_array ('curl', get_loaded_extensions())) {
echo "CURL is available on your web server\n";
}
else{
echo "CURL is not available on your web server\n";
}
print_r(get_loaded_extensions());
phpinfo();
?>
Which tell if cURL is in loaded extensions, write them down, and show classic php info details.
This is output:
There is no cURL in loaded extension. But if you check php.ini file it is C:/PHP/php.ini <- one i changed (removed ";" to enable extension.)
And when I try to run cURL commands from server I get:
Fatal error: Uncaught Error: Call to undefined function curl_multi_init() in C:\web\databaseservers\generate-db-server-stats2.php:27 Stack trace: #0 {main} thrown in C:\web\databaseservers\generate-db-server-stats2.php on line 27
(This script works locally.)
Where can be problem?
Edit1:
Adding snippet from my php.ini
There you can see that file i am editing is correct one (Path above)
And that curl is enabled as extension (PHP7.2 and above doesn't require prepone "php_" and postpone ".dll")
EDIT2:
Fixed :
for PHP7 ... I added php dir path in system enviroment varible, deleted libeay32.dll and ssleay32.dll from apache/bin, keep these dll in php directory and its work. – Wasim A. Mar 17 at 19:10
Linked topic helped

mysql is enabled in php.ini but I'm still getting Call to undefined function mysql_connect()

We have a Windows 2008 server, this morning when we came to the office, we saw that mysql is not working
We are getting this error for a simple connect function
Fatal error: Call to undefined function mysql_connect()
but the strange thing is that mysql extensions are enabled in php.ini file.
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
According to phpinfo() , php using the ini file in C:\PHP and the DLL files exist in the ext folder.
I cannot find a way to solve or at least to find out what is causing this.
I would be grateful if someone could give me some advice
Thanks in advance
for debugging purposes try
if ( !function_exists('mysql_connect') ) {
echo '<pre>mysql extension loaded: ', extension_loaded('mysql') ? 'yes':'no', "\r\n";
$cf = get_cfg_var('cfg_file_path');
echo 'ini file: ', $cf, "\r\n";
if ( !$cf || !file_exists($cf) ) {
echo "no config file\r\n";
}
else {
echo "mysql config options:\r\n";
$mc = array_filter( file($cf), function($e) { return false!==stripos($e, 'mysql') && false!==stripos($e, 'extension'); });
echo join("", $mc);
}
die('no function mysql_connect</pre>');
}
the output should be something like
<pre>mysql extension loaded: no
ini file: C:\Develop\php\php.ini
mysql config options:
;extension=php_mysql.dll
;extension=php_mysqli.dll
extension=php_pdo_mysql.dll
no function mysql_connect</pre>
which indicates that the line extension=php_mysql.dll has been commented out (in my php.ini).
In case this script shows that the php_mysql.dll indeed should have been loaded try to increase the log level of both php and your webserver and check the log file. Maybe windows couldn't load the dll because it depends on another dll which isn't present (in the correct version) anymore. E.g. the php_mysql.dll in "my" ext directory depends on MSVCR110.DLL which is present because I have Visual Studio installed, otherwise installing the Visual Studio 2012 redistributables would be required. Tools like Dependency Walker can show you which dlls are required to run an application and/or dll.
Or it could be that the php_mysql.dll implements a different API version than your php core. E.g. if you have PHP 5.4 installed but the php_mysql.dll is for php 5.1 you will see an API magic key error in the log file.
Restart Your WAMP Or XAMPP Server. You can check with Contral Panel of XAMPP Or WAMP

PDO will not run since upgrading PHP to 5.4.22

I just recently upgraded my server's PHP version to 5.4.22, and now every script that uses PDO does not work.
An example of my PHP script which won't work:
<?php
$dsn = 'mysql:dbname=testDB;host=127.0.0.1';
$user = '[hidden]';
$password = '[hidden]';
try {
$pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
?>
When i run the script i get: Fatal error: Class 'PDO' not found, i get this same error for every script that creates a connection.
I ran a check on puTTY to check PDO was even there and it found this:
root#cpanel [~]# php -m | grep -i pdo
PDO
pdo_mysql
pdo_sqlite
My php.ini has
extension=pdo.so
extension=pdo_sqlite.so
; sqlite was removed by EasyApache v3.22.24 on Sat Dec 14 23:24:10 2013 (PHP v5.4.x incompatibility)
; extension=sqlite.so
extension=pdo_mysql.so
phpinfo(); in PHP file claims im on PHP Version 5.3.10
How ever in my terminal # php -v says PHP 5.4.22
So this had really confused me why i'm getting two versions.
Your question shows that you are checking the PHP version via the command line. But PHP via a web browser is going to use a module loaded into Apache which is a completely different thing. So check the output of phpinfo(); in a PHP script loaded via the web browser. Is PDO installed or shows as installed via that?
Wherever your Apache config files are look for the directory mods-available and the file php5.load. Under Ubuntu 12.04 it would be in this path:
/etc/apache2/mods-available/php5.load
And the contents should be:
LoadModule php5_module /usr/lib/apache2/modules/libphp5.so
Does the path in that file match where the newly compiled libphp5.so is installed?
Also, that LoadModule php5_module line could be a part of your main Apache configuration. Look around to find where that is set. And then just set the path of the new module to me wherever it’s actually installed.

Fatal error: Call to undefined function pg_connect

I am using Windows 7, php 5.3.5 and WAMP server. I have two php files: trigger.php and background.php.
I want to run background.php as a background process. I have to call this file from trigger.php. To accomplish this I used below method.
I included following code in trigger.php to make background.php to process in background.
$handle = popen('start /b C:\wamp\bin\php\php5.3.5\php.exe C:\wamp\www\email3.php','r');
in background.php I have the follwing code to connect to database.
$conn_string = "host=localhost port=5432 dbname=tagbase user=postgres password=postgres";
now, on parsing this line am getting the follwing error :
Fatal error: Call to undefined function pg_connect() in C:\wamp\www\background.php on line 3 Call Stack: 0.0002 322792 1. {main}() C:\wamp\www\background.php:0
By searching in in the internet I found some solutions, and made changes as recommended below in php.ini,
uncommented, extension=php_pdo_pgsql.dll,
uncommented, extension=php_pgsql.dll,
uncommented, extension_dir = "c:/wamp/bin/php/php5.3.5/ext/",
also I do have php_pdo_pgsql.dll and php_pgsql.dll files in c:/wamp/bin/php/php5.3.5/ext/ folder.
Any suggestions are appreciated.
Apache 2.2.X configuration
Add the next line to the Apache 2.2.x httpd.conf configuration:
LoadFile "C:/Program Files/PostgreSQL/{version}/bin/libpq.dll"
Above line must be entered before the next line.
LoadModule php5_module "c:/wamp/bin/php/php5.2.11/php5apache2_2.dll"
PHP 5.2.X Configuration
Enable the following two lines in the php.ini configuration file. By 'Enable' i mean, remove trailing ; (semicolon). By these, you un-comment the line.
extension=php_pdo_pgsql.dll
extension=php_pgsql.dll
Restart WAMP
Test by adding this in your index.php
echo extension_loaded('pgsql') ? 'yes':'no';
(source: http://www.plaatsoft.nl/wamp-postgresql-integration/)
if we install WAMP server, we will get two php.ini files.
one in C:\wamp\bin\php\php5.3.5 and another in C:\wamp\bin\apache\Apache2.2.17\bin.
if we execute a php file from browser, then php.ini file in C:\wamp\bin\apache\Apache2.2.17\bin will be referred.
if we execute a php file from command line, then php.ini file in C:\wamp\bin\php\php5.3.5 will be referred.
what ever the changes i made enable/disable the dll in PHP->PHP extensions,all the changes will be saved in
C:\wamp\bin\apache\Apache2.2.17\bin\php.ini file. but C:\wamp\bin\php\php5.3.5\php.ini file remains unchanged.
for my problem, i made the following changes
uncommented, extension=php_pdo_pgsql.dll,
uncommented, extension=php_pgsql.dll,
uncommented, extension_dir = "c:/wamp/bin/php/php5.3.5/ext/",
in C:\wamp\bin\php\php5.3.5\php.ini file. Now its working good. :)
In my case with
Apache/2.4.10
PHP Version 5.4.32
PostgeSQL 9.3
I added libpq.dll into Apache httpd.conf from PHP catalog because Apache refused to start with libpq.dll from PostgreSQL 9.3

Categories