Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

PHP pg_connect to PostgreSQL

Name: Anonymous 2013-12-21 6:44

Trying to use PHP to remotely query a PostgreSQL DB on my AWS server..


<?php

    if ( isset($_POST["searchText"]) )
    {
        $searchText  = $_POST["searchText"];
        $searchType  = $_POST["searchType"];
        $searchDb  = $_POST["searchDb"];
    }
   
    $query = "SELECT * FROM " . $searchDb . " WHERE " . $searchType . " = '" . $searchText . "';";
   
    $queryResult = query($searchDb, $query);
    //echo $queryResult;
 
    function query($searchDb, $query)
    {
        $host = "wouldntyouliketoknow.com";
        $user = "username";
        $pass = "password";
        $db = "database";
       
        pg_connect("host=$host port=5432 dbname=$db user=$user password=$pass");
       
        if (!$conn)
        {
            echo "Not connected";
        }
   
        $result = pg_query($conn, $query);
        pg_close($conn);
        return $result;
    }
   
?>


It doesn't throw any errors, the connection request just times out every time after like 15 seconds, then it just echoes "Not connected." Is this an issue with the AWS server, or the server I'm trying to open the connection from?

Name: Anonymous 2013-12-21 8:27

install gentoo

Name: Anonymous 2013-12-21 9:29

Jesus christ, that's insecure as hell, man.
I suppose you're still learning, but as a rule you should _never_ create query strings like that.
Use prepared statements or whatever they're called in your language of choice.

Sounds like it's an issue with the server.
But I don't really know

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-12-21 9:34

>>3
OP needs to use this: http://php.net/mysql_real_escape_string

(Ignore that it's MySQL, any *SQL needs the same escaping done. There is no postgresql_real_escape_string.)

Name: Anonymous 2013-12-21 10:19

>>3
>>4

Thanks, this is just temporary working code until I get around to actually doing it properly..

but regardless of the code, I can't get pg_connect to actually connect :S

Name: Anonymous 2013-12-21 19:53

>>5
Optimise your quotes please!

Name: Anonymous 2013-12-22 5:34

>not using couchdb/reddis/mongodb/nosql
lel stay pleb

Name: Anonymous 2013-12-22 8:41

>>1,5
You aren't actually setting your $conn variable anywhere, genius.
Apart from that, if you're ever using pg_query instead of pg_query_params you shouldn't be allowed to be near a computer, and if you're creating a new database connection for every query, your server will feel the same way.

Name: Anonymous 2013-12-22 8:44

>You aren't actually setting your $conn variable anywhere, genius.
Actually I am, that was just copypasta fail.

/prog/ is useless

Name: Anonymous 2013-12-22 8:47

>>9
You can't even copy/paste your code properly, and you blame me for not reading your mind? Are you here because Stack Overflow banned you? If not, go back there.

Name: Anonymous 2013-12-22 9:12

>>10

Why would I be banned from Stack Overflow? I'm not an aspie like the majority of posters here. As I've said before, the issue is not the code. Prepared statements, non-prepared statements, nigger functions, it makes no difference. The problem is that it times out every time I try to open the database connection.


<?php
    if ( isset($_POST["searchText"]) )
    {
        $searchText  = $_POST["searchText"];
        $searchType  = $_POST["searchType"];
        $searchDb  = $_POST["searchDb"];
    }
   
    $query = "SELECT * FROM " . $searchDb . " WHERE " . $searchType . " = '" . $searchText . "';";
   
    $queryResult = query($searchDb, $query);
    echo $queryResult;
 
    function query($searchDb, $query)
    {
        $host = "fatniggers.com";
        $user = "niggeruser";
        $pass = "niggerpass";
        $db = "niggerdb";
       
        $dbconn = pg_connect("host=$host port=5432 dbname=$db user=$user password=$pass connect_timeout=3<") or die("Could not connect");
       
        $result = pg_prepare($dbconn, "searchQuery", $query);
        $result = pg_execute($dbconn, "searchQuery", array("Search Result")) or die(pg_last_error($dbconn));
       
        pg_close($dbconn);
       
        return $result;
    }   
?>

Name: Anonymous 2013-12-22 9:18

>>11
Before some dumbshit mentions it:
connect_timeout=3<")
Yes, I noticed this. No, it doesn't matter.

Name: Anonymous 2013-12-22 9:46

>>11
the issue is not the code.
Clearly posting more code is going to solve your problem, then. Jesus fuck, PHP ``programmers''.

Don't change these.
Name: Email:
Entire Thread Thread List