如何通过 PHP 连接到 PotgreSQL 数据库?

发布于 2024-10-16 06:06:54 字数 6142 浏览 0 评论 0原文

如何通过 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 "&nbsp;&nbsp;$i.<a href='song.php?cat=" . $catid . "&id=" . $play['id'] . "'>" . $play[author] . "-" . $play[name] . "</a><br />";
        }
        echo "</div>";
    }
?>

How can I connect with my PostgreSQL database trough PHP? I try this

$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);

But it doesen't seems to work. Here is the entire code, can someone help me:

    <?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>";
    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

优雅的叶子 2024-10-23 06:06:54

检查 Postgres pg_connect 页面:您需要在连接字符串中指定数据库名称,而函数 pg_database 似乎根本不存在。

Check the Postgres pg_connect page: You need to specify the database name in the connection string and the function pg_database does not seem to exist at all.

天气好吗我好吗 2024-10-23 06:06:54

我建议使用数据库抽象库。我喜欢 ADOdb,但还有其他的。这使得将代码移植到 mySQL 或其他数据库变得更加容易,并隐藏了调用 pg_? 的一些复杂性。直接发挥作用。

还可以尝试使用空字符串 '' 作为主机和端口。由于您连接到 localhost,服务器可能只接受 unix 套接字连接,而不接受 TCP 连接。

正如 Frank 指出的那样,没有错误,所以也许您的连接正常,但您的 SQL 语句是错误的?

I would suggest using a database abstraction library. I like ADOdb but there are others. This allows porting the code to say mySQL or other database much easier and hides some of the complexities of calling pg_? functions directly.

Also try using empty string '' for the host and port. Since your connecting to localhost it might be the server only accepts unix socket connections and not TCP connections.

As Frank noted there is no error so maybe your connection is working and your SQL statement is wrong?

痴梦一场 2024-10-23 06:06:54

打开错误报告并向他们显示:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// rest of your code
?>

您是否使用扩展名 php_pgsql 更改了 php.ini?这个扩展也有效吗?检查 phpinfo() 以确定。

编辑:您没有对 PostgreSQL 的结果执行任何操作,对吗?

Turn error reporting on and show them:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// rest of your code
?>

Did you change php.ini use the extension php_pgsql? And is this extension working as well? check phpinfo() to be sure.

Edit: You don't do anything with the results from PostgreSQL, is that correct?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文