/** * Update data with param * \param[in] table name of table * \param[in] data associative - array of field=>value * \param[in] cond conditions - array of field=>value */ function update($table, $data, $cond, $options = PGSQL_DML_EXEC) { $res = pg_update($this->conn, $table, $data, $cond, $options); if (!$res) { throw new Exception(DB_ERR . 'Update: ' . pg_last_error($this->conn)); } return $res; }
function dbUpdate($table_name, $data, $id) { global $database_connection; $result = pg_update($database_connection, $table_name, $data, array('id' => $id)); if (!$result) { die("PG Error: " . pg_result_error($result)); } }
function update($table, $data, $criteria) { return $this->q(pg_update($this->connection, $table, $data, $criteria, PGSQL_DML_STRING)); }
function DB_update_connected_user($username, $DB_params) { $card_id = sspmod_InfoCard_UserFunctions::generate_card_ID($username); $dbconn = pg_connect('host=' . $DB_params['DB_host'] . ' port=' . $DB_params['DB_port'] . ' dbname=' . $DB_params['DB_dbname'] . ' user='******'DB_user'] . ' password='******'DB_password']); $result = pg_fetch_all(pg_query_params($dbconn, 'SELECT * FROM connected_users WHERE name = $1', array("{$username}"))); if ($result[0]) { pg_update($dbconn, 'connected_users', array('card_id' => $card_id), array('name' => $username)); return true; } else { return false; } }
<?php $DB_host = 'localhost'; $DB_port = '5432'; $DB_dbname = 'db1'; $DB_user = '******'; $DB_password = '******'; $username = '******'; $card_id = '1234567'; $dbconn = pg_connect("host={$DB_host} port={$DB_port} dbname={$DB_dbname} user={$DB_user} password={$DB_password} "); $result = pg_fetch_all(pg_query_params($dbconn, 'SELECT * FROM connected_users WHERE name = $1', array("{$username}"))); if ($result[0]) { pg_update($dbconn, 'connected_users', array('card_id' => $card_id), array('name' => $username)); print_r($result); } else { echo 'error'; } // echo pg_last_error($dbconn); // if (!$result) { // echo 'FALLO'; // } else { // print "result: $result </br>"; // $row=pg_fetch_all($result); // print "ROW: $row </br>"; // // print_r ($result); // print_r ($row); // } pg_close($dbconn); // $handle = fopen(SimpleSAML_Utilities::getTempDir() . '/prueba2.txt','w'); // fwrite($handle, 'prueba'); // fclose ($handle);
<?php require_once 'config.inc'; $dbh = @pg_connect($conn_str); if (!$dbh) { die("Could not connect to the server"); } pg_query("CREATE TABLE php_test (id SERIAL, tm timestamp NOT NULL)"); $values = array('tm' => 'now()'); pg_insert($dbh, 'php_test', $values); $ids = array('id' => 1); pg_update($dbh, 'php_test', $values, $ids); pg_query($dbh, "DROP TABLE php_test"); pg_close($dbh); ?> ===DONE===
<?php include 'config.inc'; $conn = pg_connect($conn_str); pg_query('CREATE SCHEMA phptests'); pg_query('CREATE TABLE foo (id INT, id2 INT)'); pg_query('CREATE TABLE phptests.foo (id INT, id2 INT)'); pg_insert($conn, 'foo', array('id' => 1, 'id2' => 1)); pg_insert($conn, 'phptests.foo', array('id' => 1, 'id2' => 2)); pg_update($conn, 'foo', array('id' => 10), array('id' => 1)); var_dump(pg_update($conn, 'foo', array('id' => 10), array('id' => 1), PGSQL_DML_STRING)); pg_update($conn, 'phptests.foo', array('id' => 100), array('id2' => 2)); var_dump(pg_update($conn, 'phptests.foo', array('id' => 100), array('id2' => 2), PGSQL_DML_STRING)); $rs = pg_query('SELECT * FROM foo UNION SELECT * FROM phptests.foo'); while ($row = pg_fetch_assoc($rs)) { var_dump($row); } pg_query('DROP TABLE foo'); pg_query('DROP TABLE phptests.foo'); pg_query('DROP SCHEMA phptests');
function sql_query_build($query_type, $array, $table) { if (!is_array($array) || !$table) { return false; } $debug_backtrace = debug_backtrace(); $backtrace = array(); // remove the root directorys $backtrace['file'] = str_replace('\\', '/', $debug_backtrace[0]['file']); $backtrace['file'] = str_replace(SITE_FILE_ROOT, '', str_replace($_SERVER['DOCUMENT_ROOT'], '', $backtrace['file'])); $backtrace['line'] = $debug_backtrace[0]['line']; $this->num_queries++; $this->last_query = ''; switch (strtoupper($query_type)) { case 'MULTI_INSERT': foreach ($array as $array2) { //pg_insert($this->link_identifier, $table, $array2); $this->query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $array2), $backtrace); } break; case 'INSERT': // return pg_insert($this->link_identifier, $table, $array); // We can't use pg_insert if we need needed to get the insert_id $this->last_query = 'INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $array); $this->last_result = $this->query($this->last_query, $backtrace); return $this->last_result; break; case 'UPDATE': return pg_update($this->link_identifier, $table, $array); //return $this->query('UPDATE ' . $table . ' SET ' . $array); break; } }
<?php require_once 'config.inc'; $dbh = pg_connect($conn_str); $tbl_name = 'test_47199'; @pg_query("DROP TABLE {$tbl_name}"); pg_query("CREATE TABLE {$tbl_name} (null_field INT, not_null_field INT NOT NULL)"); pg_insert($dbh, $tbl_name, array('null_field' => null, 'not_null_field' => 1)); pg_insert($dbh, $tbl_name, array('null_field' => null, 'not_null_field' => 2)); var_dump(pg_fetch_all(pg_query('SELECT * FROM ' . $tbl_name))); $query = pg_delete($dbh, $tbl_name, array('null_field' => NULL, 'not_null_field' => 2), PGSQL_DML_STRING | PGSQL_DML_EXEC); echo $query, "\n"; $query = pg_update($dbh, $tbl_name, array('null_field' => NULL, 'not_null_field' => 0), array('not_null_field' => 1, 'null_field' => ''), PGSQL_DML_STRING | PGSQL_DML_EXEC); echo $query, "\n"; var_dump(pg_fetch_all(pg_query('SELECT * FROM ' . $tbl_name))); @pg_query("DROP TABLE {$tbl_name}"); pg_close($dbh); echo PHP_EOL . "Done" . PHP_EOL;
<?php error_reporting(E_ALL); include 'config.inc'; $db = pg_connect($conn_str); pg_query($db, "SET standard_conforming_strings = 0"); $fields = array('num' => '1234', 'str' => 'ABC', 'bin' => 'XYZ'); $ids = array('num' => '1234'); pg_update($db, $table_name, $fields, $ids) or print "Error in test 1\n"; echo pg_update($db, $table_name, $fields, $ids, PGSQL_DML_STRING) . "\n"; echo "Ok\n";
case "insert": $sql = "insert into " . $table . "(" . implode(", ", array_keys($values)) . ") values (" . implode(", ", array_values($values)) . ");"; $result = pg_query($conn, $sql); if ($result == false) { $result = pg_last_error(); } else { $result = true; } if ($table == 'public.location_user' || $table == 'public.group_user') { break; //relasjonstabeller skal ikke returnere noen id } $result = pg_query($conn, "select currval('" . $table . "_" . $json_object->table . "_id_seq');"); break; case "update": $result = pg_update($conn, $table, $values, $conditions); break; case "select": $sql = "select " . implode(", ", $to_select) . " from " . $table . " where " . implode(" and ", $conditions_array) . ";"; $result = pg_query($conn, $sql); break; case "delete": $result = pg_delete($conn, $table, $conditions); break; case "sql": $result = pg_query($conn, $sql); $str = "["; while ($result_row = pg_fetch_assoc($result)) { $str .= json_encode($result_row) . ","; } $str = rtrim($str, ",");