开发者

How do I connect to PotgreSQL Database trough PHP?

开发者 https://www.devze.com 2023-02-08 03:28 出处:网络
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\");

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://m开发者_Go百科edia03.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>";
    }
?>


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.


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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消