Tuesday, April 13, 2010

getIdInsert


$loadFile = "bxsc.trans.cards.dat";
$dbconn = array("user"=>"djmornyc","host"=>"localhost","dbname"=>"mor_db");

if(!$conn = getConn($dbconn))
echo "connection failed\n";

$fp = fopen($loadFile,"r");
$null = pg_query($conn,"BEGIN");

while($data = fgets($fp,4096))
{
$cardnum = trim($data);
$resInsert = pg_query($conn,getSqlInsertCard($cardnum));
var_dump(pg_fetch_all($resInsert))."\n";
}

$null = pg_query($conn,"COMMIT");
$null = fclose($fp);
$null = pg_close($conn);

function getSqlInsertCard($cardnum)
{
return "insert into bxsc_cards (cardnum)
select '{$cardnum}' where not exists (select cardnum from bxsc_cards where cardnum = '$cardnum') returning id";

}

function getConn($dbconn)
{
$connStr = "";

foreach($dbconn as $key => $value)
$connStr.=" {$key}={$value} ";

return pg_connect($connStr);
}

?>

No comments: