i wanna to connect to postgresql database using php but it display a blank screen when submit and doesnt execute the query
Thanks in advance
Update: this is my code
function execute_query($dbName,$query){
$host = "localhost";
$user = "postgres";
$pass = "";
$db = "test";
echo "before create connection";
$con = pg_connect ("host=$host dbname=$db user=$user password=$pass");
echo "After connection is created";
if (!$con) {
echo "not connected";
// die('Could not connect: ' . mysql_error());
}
$result = pg_query($con, $query);
pg_close($con);
return $result;
}
The output:
display the message "before connection" but doesn't display the message "After connection is created" or "not connected".
The problem is likely a PHP error that’s getting recorded to a log file somewhere. Locate the log file, or enable showing the log errors on your page by using the following at the top of your script:
ini_set('display_errors', '1');
error_reporting(E_ALL | E_STRICT);
This is a short-term solution and can’t be used in deployment (where you want to set display_errors to 0). For a long-term solution, you really want to locate the Apache or PHP error log and tail it.
To try to find the error log, run the following script:
<?php phpinfo(); ?>
In the Configuration > PHP Core section, look for error_log. If that’s not set, you can set it in your php.ini file. All errors will be recorded to that file, even if you have display_errors set to 0.
Add a php file to your server and put this in that file:
<?php
echo phpinfo();
?>
When you open that file from the browser, check if postgres support is setup for php.
you should something like this on the page:
pgsql
PostgreSQL Support enabled
PostgreSQL(libpq) Version 8.2.3
Multibyte character support enabled
SSL support disabled
Active Persistent Links 0
Active Links 0
Try checking your web server's error log (for instance, /var/log/apache2/error.log is a common location for the Apache2 log) and see if there is an error reported from PHP there.
You may want to set the php error reporting level in your ini file if this is a local development server.
It seems, that your php does not have postgresql support, cf. http://us.php.net/manual/en/ref.pgsql.php:
Note: Not all functions are supported
by all builds. It depends on your
libpq (The PostgreSQL C client
library) version and how libpq is
compiled. If PHP PostgreSQL extensions
are missing, then it is because your
libpq version does not support them.
Related
I could use some help determining what is the best way for me as the developer to see what is causing an error as I am trying to connect to a progress openedge database.
All I am seeing is that the connection is failing based on the die message.
In short I am simply looking for more verbose error logging/messaging from the odbc_connect function.
I have a production computer that this is running on just fine, so I would like to know on my new development machine what is missing.
$my_port = "3500";
$my_username = "my_username";
$my_password = "my_password";
$my_database = "my_db";
//0 (READ UNCOMMITTED)
//1 (READ COMMITTED)
//2 (REPEATABLE READ)
//3 (SERIALIZABLE)
$my_dil = 0;
$my_connection = odbc_connect(
"Driver={Progress OpenEdge 11.7 Driver};
HOST=$progress_host;
PORT=$my_port;
DB=$my_database;
UID=$my_username;
PWD=$my_password;
DIL=$my_dil", "", ""
) or die ("ERROR: Could not connect to Progress OpenEdge Database.");
So I figured it out how to connect. I know that doesn't give us error handling, but it does allow us to actually connect.
I added the following to php.ini and validated they were in the ext folder; then restarted iis.
extension=php_pdo_odbc.dll
extension=php_ldap.dll
extension=php_odbc.dll
I needed to install the drivers on my development box. I did this by moving the progressx86 folder from my working server.
I then ran the following file.
C:\Progressx86\OpenEdge\install\odbc\sql-odbc-setup.exe
Note:
There might be other steps that I tried before this one that affected the outcome, but before I did my answer above it didn't work, and after I did my answer above it worked.
hello to everyone here ... please i need your help . i have some trouble with php and my SGBDs ... i have installed php mysql and apache separately . but when i launch a php program with mysql_connect it send a 500 internal error without response . i have changed mysql to postgresql but it is the same problems . i have also remove the comma before some line in php.ini but it's the same result ... there is a simple connection to the database code examples `
<?php
$link = pg_connect('localhost', 'USERNAME', 'PASSWORD');
if (!$link) {`
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
pg_close($link);
`
and there is the result the result of the code with 500 error
and there is my php.ini file image of my php.ini
thanks you
According to http://php.net/manual/en/function.pg-connect.php, you need to do
pg_connect("host=localhost dbname=mary user=USERNAME password=PASSWORD");
So the format is not correct and dbname is missing from your code.
Have you restart apache ?
Set display_errors directive to On in your php.ini
Create info.php in your document root, put <? phpinfo(); ?> into it.
Open info.php from your browser, try to find word "pdo_pgsql" / "pdo_mysql". Has it appear correctly ?
Dig into your apache access_log and error_log files to get further clue about your error.
I have a problem accessing a mongoDB database using the PHP driver.
When I locally run the following code:
<?php
echo "Connecting";
$manager = new MongoDB\Driver\Manager("mongodb://localhost:28124");
echo "Connected";
?>
it works.
However, when I access this php file remotely from the browser, I only see "Connecting" and then the web page hangs with internal server error 500.
I'm running with:
PHP 5.6.25 (cli)
MongoDB: 1.1.8
Apache: 2.2.22
Any ideas?!?!?!?
There is no way of tracing the problem without logs.
Please check your default log, which should be in "/var/log/httpd/error_log" or "/var/log/apache2/error.log" (custom logs might not get all the info) and provide specifics.
If you are not getting anything from the logs, please retry after turning PHP logging on.
In php.ini(of apache dir):
Remove semicolon from in front of below lines
;display_errors
;display_startup_errors
;error_reporting
Set the values to
display_errors = On
display_startup_errors = On
error_reporting = E_ALL
Restart the server, run script, on error, check the logs again.
Note: Reset the php.ini to default values after you are done. Error logging adds significant overhead to execution.
<?php
//database connectivity
$connect_error='Sorry We could not able to connect to the database';
mysql_connect('localhost','root','') or die($connect_error);
mysql_select_db('beehive_intwebpage') or die ($connect_error);
?>
We have this in setup this in our localhost. When there no connection with the database we get the error along with the error message.
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in D:\core\database\connect.php on line 3
Sorry We could not able to connect to the database
How to show only the message and not the default sql error.
Thanks!
First: Don't use mysql_*-methods anymore, they're deprecated. Instead use mysqli or PDO.
For your error, disabling the error-reporting according to this documentation should help.
Just add error_reporting(0); to the beginning of your file.
Hey default errors are caused due to php itself, you can turn them off in the php configuration file and you have to turn them off really because watching errors give an attacker knowledge of how your site works and your sites internal structure. So just turn off the error_reporting in php configuration
So I have a PHP application running well on my local server, powered by Zend Server. After I uploaded it to the ubuntu server, it stops working, except for two php file, the index.php and register.php, which handles the user registration part. When I say working, it means I can view the page as in my local server but the internal process is not functioning. For example, when I try to register a user, it should tell me whether it is successful or not. Instead, it is a just a blank page.
I rule out the directory problem. All the path are relative and they are working fine. I also rule out the MySQL database problem as I can access the database and do everything with it. So I wrote the following script to test where php can access mySQL (the MySQL database is set up in the same host). So I wrote the following script:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
require_once("config/connect_database_viewer.php");
echo $db_hostname;
echo "<br>";
echo "1";
echo $db_username;
$sql = "CREATE TABLE writer (UPC VARCHAR(15)) ENGINE MyISAM";
if ($db_server->query($sql)) {
echo "the database works";
} else {
echo "so it didn't even reach the server";
}
Fatal error: Fatal error: Class 'mysqli' not found in /var/www/viewer/angelo/config/connect_database_viewer.php on line 3.
But it doesn't make sense to me as I tried to install php5-mysql by this command:
sudo apt-get install php5-mysql
it comes back to me saying php5-mysql is already the newest version. And I checked the ubuntu page for the version, the php5-mysql module should include a mysqli extension. The php module is enabled too! Here is my connect_database_viewer.php file:
<?php
$db_hostname = '127.0.0.1';
$db_database = 'viewer';
$db_username = 'juvo1';
$db_password = 'juvo1';
$db_server = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($db_server->connect_error) die($db_server->connect_error)
?>
You need to check the error log. It'll tell you where the error happened in your PHP script. Try looking in
/var/log/httpd/error.log
and see what it says.
If only the first line of your test script is printed, your include script creates an error. The reason can be that your include fails (maybe due to rights problem) or something inside the script. I would include the
ini_set("display_errors", 1);
error_reporting(E_ALL);
to get the errors in the browser if your console (= error log?) does not mention anything else.
It appears that there are two problem in the process. The first one is that there are no mysqli module built-in for my version of php. People can install the php mysqli module with this command:
sudo apt-get install php5-mysqlnd
This stops the white page from showing up. But there is still warning about trying to connect via tcp. So I change the database conf php file from 127.0.0.1 to localhost. For more information, please refer to this link: mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2).