Home » Php » PHP – How to update some values in postgresql table

PHP – How to update some values in postgresql table

Posted by: admin October 29, 2018 Leave a comment

Questions:

How do you update some values in postgresql table?

I have a large database with a lot of columns and within this column a lot of values. I would like to update only when the values aren’t up to date any more. But I can’t tell which one need to be updated because of the size. My code just updates all values but because of the large number of data it will take to long. Is there a way to specify, to determine which values need to be updated.

I though I could work on this principal ==> For example if a value in column sort is changed, the rest of the row will be updated automatically. In this way, it doesn’t have to search for any changes in each column.

$host = "localhost";
$username = "postgres";
$password = "password";
$dbname = "dbtest";   
$dbh = new PDO("pgsql:dbname=$dbname; host=$host", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($dbh->connect_error) {
    die("Connection failed: " . $dbh->connect_error);
}

# Query used to insert data
# $sql = "INSERT INTO test (id,sort,full, lm, gs, sp, gan, pac, gsyn, type, searchterm, inter, pat, go) VALUES ('".$id."','".$name ."','".$full_name."','".$lm ."','".$genesymbol ."','".$specie ."','".$geneacc ."','".$placc ."','".$genesyno ."','".$type ."','".$searchterm ."','".$intera ."','".$path ."','".$geneon ."')";

# Query used to update
UPDATE species SET sort ='".$name ."', full_name='".$full_name."' , lm='".$lm ."', gs='".$genesymbol ."', sp='".$specie ."', gan='".$geneacc ."', pac='".$placc ."', gsyn='".$genesyno ."', type='".$type ."', searchterm='".$searchterm ."', inter='".$intera ."', pat='".$path ."', go='".$geneon ."' WHERE id = '".$id."' ";

    if ($dbh->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $dbh->error;
}
$conn->close();
Answers: