如何通过PHP连接我的PostgreSQL数据库?我试试这个
$PG_Con = pg_connect("host=127.0.0.1 port=5432 user=user password=pass");
pg_database("db");
$PG_Users = pg_query($PG_Con, "SELECT * FROM users_mfuser WHERE user_ptr_id='71'");
$user2 = pg_fetch_assoc($PG_Users);
但它似乎不起作用。这是整个代码,有人可以帮助我:
<?php
include "include/functions.php";
$id = (int) $_GET['id'];
$catid = (int) $_GET['cat'];
if ($catid == NULL) {
echo "Променливата е празна !";
} else {
print functionCat("" . $catid . "");
functionConnect("pozdrawi");
$SQL_Song = mysql_query("SELECT * FROM songs WHERE id='$id'");
$row = mysql_fetch_array($SQL_Song);
$url = $row['link'];
$number = explode(":", $url);
$i = substr($number[2], 0, 2);
$link1 = "http://media.vbox7.com/s/$i/$number[2].flv";
if (@fopen("http://media.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media02.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media02.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media03.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media03.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media04.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media04.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media05.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media05.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media06.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media06.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media07.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media07.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media08.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media08.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media09.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media09.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media10.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media10.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media11.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media11.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media12.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media12.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media13.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media13.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media14.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media14.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media15.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media15.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media16.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media16.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media17.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media17.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media18.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media18.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media19.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media19.vbox7.com/s/$i/$number[2].flv";
} elseif (@fopen("http://media20.vbox7.com/s/$i/$number[2].flv", "r")) {
$adres = "http://media20.vbox7.com/s/$i/$number[2].flv";
}
mysql_close();
$PG_Con = pg_connect("host=127.0.0.1 port=5432 user=user password=pass");
pg_database("db");
$PG_Users = pg_query($PG_Con, "SELECT * FROM users_mfuser WHERE user_ptr_id='71'");
$user2 = pg_fetch_assoc($PG_Users);
if ($user2['user_song_credits'] == 0) {
print "<center>asd</center>";
} elseif ($user2['user_song_credits'] > 0) {
?><br />
<center><div id='mediaspace'>Трябва да имате инсталиран Flash Player за да слушате</div></center>
<script type='text/javascript'>
var so = new SWFObject('player.swf','mpl','350','20','9');
so.addParam('allowfullscreen','true');
so.addParam('allowscriptaccess','never');
so.addParam('wmode','opaque');
so.addVariable('backcolor','8454c6');
so.addVariable('frontcolor','FFFFFF');
so.addVariable('lightcolor','FFFFFF');
so.addVariable('screencolor','0f0f0f');;
so.addVariable('menu','over');
so.addVariable('file','<?= $adres ?>&menu=false');
so.write('mediaspace');
</script><br />
<?php
pg_close($PG_Con);
functionConnect("pozdrawi");
echo "Сега свири: <b>".$row['author']." - ".$row['name']."</b>";
}
$i = 0;
$Play_Song = mysql_query("SELECT * FROM songs WHERE cat_id='$catid'");
echo "<center>
<a href='javascript: show()'>Playlist</a><br />
<div class='playlist' align='left' style='display:none;'>";
while ($play = mysql_fetch_array($Play_Song)) {
$i = $i + 1;
$song_cat = $play['cat_id'];
$Play_Cat = mysql_query("SELECT * FROM songs_cat WHERE id='$song_cat'");
$cat = mysql_fetch_array($Play_Cat);
$category = $cat['cat_name'];
echo " $i.<a href='song.php?cat=" . $catid . "&id=" . $play['id'] . "'>" . $play[author] . "-" . $play[name] . "</a><br />";
}
echo "</div>";
}
?>
答案 0 :(得分:2)
检查Postgres pg_connect
页面:您需要在连接字符串中指定数据库名称,而函数pg_database
似乎根本不存在。
答案 1 :(得分:0)
启用错误报告并显示它们:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
// rest of your code
?>
您是否更改了php.ini使用扩展名php_pgsql?这个扩展是否正常运行?检查phpinfo()以确定。
编辑:你没有对PostgreSQL的结果做任何事情,这是正确的吗?
答案 2 :(得分:0)
我建议使用数据库抽象库。我喜欢ADOdb,但还有其他人。这允许移植代码更容易说mySQL或其他数据库并隐藏调用pg_的一些复杂性?直接起作用。
还尝试使用空字符串''作为主机和端口。由于您连接到localhost,因此服务器可能只接受unix套接字连接而不接受TCP连接。
正如Frank所说,没有错误,所以你的连接可能正常,你的SQL语句错了吗?