我正在开发一个在我的PostgreSQL数据库上运行查询的PHP代码:
$insert = pg_query($pg_conn,
"INSERT INTO kullanicilar(id, u_name, u_firstname, u_lastname, register_date, gold, exp, play_times, level)
VALUES($k_id, '$u_name', '$u_firstname', '$u_lastname', 'NOW()', 0, 0, 0, 1);"
);
if ($insert) {
echo 'Insert succeeded.';
} else {
echo 'An error occurs when inserting';
}
我收到每次插入消息时都会出错。我的代码出了什么问题?
-Edit-(详情)
我正在使用Heroku Postgres。我的变量的定义是这样的;
$k_id = intval($basic['id']); //int
$u_name = $basic['name']; //string
$u_firstname = $basic['first_name']; //string
$u_lastname = $basic['last_name']; //string
这是我的heroku日志的一部分:
PHP Warning: pg_pconnect(): Unable to connect to Postg
reSQL server: could not connect to server: Connection refused\n\tIs the server r
unning on host "-----.amazonaws.com" and accepting\n\tTCP/
IP connections on port 5432? in /app/www/index.php on line 91, referer: http://a
pps.facebook.com/---/
PHP Warning: pg_last_error() expects parameter 1 to be
resource, boolean given in /app/www/index.php on line 119, referer: http://apps
.facebook.com/---/
PHP Warning: pg_num_rows() expects parameter 1 to be r
esource, null given in /app/www/index.php on line 106, referer: http://apps.face
book.com/---/
PHP Warning: pg_query() expects parameter 1 to be reso
urce, boolean given in /app/www/index.php on line 107, referer: http://apps.face
book.com/---/
PHP Warning: pg_last_error(): No PostgreSQL link opene
d yet in /app/www/index.php on line 111, referer: http://apps.facebook.com/---/
PHP Warning: pg_query() expects parameter 1 to be reso
urce, boolean given in /app/www/index.php on line 98, referer: http://apps.faceb
ook.com/---/
答案 0 :(得分:1)
如果register_date
应该是timestamp
(带时区),则应为now()
(不带单引号,函数调用)而不是'now()'
(单引号,字符串文字)。
Postgres发出更详细的错误消息。检查数据库日志。
pgAdmin
只是一个GUI。如果它驻留在客户端上,它可能根本无法访问服务器上的日志文件。如果它位于同一台机器上,则只需正确配置即可。
每个正常配置的Postgres服务器都会写入日志文件。查看postgresql.conf
at the settings described here以查找或配置位置。