RE: [PEAR] DB Error: constraint violation

From: Date: Wed, 18 Dec 2002 07:16:57 +0000
Subject: RE: [PEAR] DB Error: constraint violation
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-3032@lists.php.net to get a copy of this message
I assume it means that one of your queries violates a contraint that you set in the database (like uniquieness of values in the primary key column). I would recomment good old debugging tactics and simply manually checking each query on the console until you get your constraint violation. Whenever you output pear errors you may also want to add the __LINE__ constant and/or $[name of the variable containing the pear error]->getUserinfo(). This should help you debug. Finally there is MDB (http://pear.php.net/package-info.php?package=MDB). Which already provides what you are trying here with the replace() method. Regards, Lukas Smith smith@dybnet.de _______________________________ DybNet Internet Solutions GbR Reuchlinstr. 10-11 Gebäude 4 1.OG Raum 6 (4.1.6) 10553 Berlin Germany Tel. : +49 30 83 22 50 00 Fax : +49 30 83 22 50 07 www.dybnet.de info@dybnet.de > -----Original Message----- > From: Shane Antyr [mailto:santyr@macosx.com] > Sent: Wednesday, December 18, 2002 5:33 AM > To: pear-general@lists.php.net > Subject: [PEAR] DB Error: constraint violation > > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi, > > I'm gettiing a ' DB Error: constraint violation' when I run the > following function against the following table. Note the error only > happens on an insert. Anyone have any ideas, or know what this means? > > Thanks in advance. > > Shane Antyr > > /** > * insertOrUpdate() V1 > * > * @param dbh database handler > * @param table table to query > * @param values array: values to enter into the database > * @param id db record key > * > */ > function insertOrUpdate($dbh, $table, $values, $id) > { > /* run a query and get number of columns for $table */ > // define query > $result = $dbh->query("SHOW COLUMNS FROM ".$table.""); > > // get results > // build an array of column names > while($columns = $result->fetchRow()){ > $colName[] =$columns[0]; > } > > // ERROR > if(PEAR::isError($result)){ > echo"get column count for ".$table." failed:\n\n"; > dbError($result); > } // end ERROR > > // get how many columns there are > // used to build subsequent queries > $count = count($colName); > > /* check to see if the record exists */ > // define query > $stmt = "SELECT id FROM ".$table." WHERE id = > '".$id."'"; > > // send query > $result1 = $dbh->query($stmt); > > // get results > $row = $result1->fetchRow(DB_FETCHMODE_ORDERED); > > // ERROR > if(PEAR::isError($result)){ > echo"get id from ".$table." failed:\n\n"; > dbError($result); > } // end ERROR > > /* the record exits. update it */ > if($row[0]){ > // create columns for query, required by prepare() > for($i=1; $i < $count -1; $i++){ > $columns .= $colName[$i]."=?, "; > > } > > // last column > $columns .= $colName[$i]."=?"; > > $sth = $dbh->prepare("UPDATE ".$table." SET ".$columns. > " WHERE id = '".$id."'"); > $result3 = $dbh->execute($sth, $values); > > // ERROR > if(PEAR::isError($result3)){ > echo $table." update failed:\n\n"; > dbError($result3); > } // end ERROR > > } // end if > > /* record doesn't exist, insert the data */ > else{ > // create columns for query, required by prepare() > for($i=0; $i < $count -2; $i++){ > $columns .= "?, "; > > } > > $columns .= "?"; > > $sth = $dbh->prepare("INSERT INTO ".$table." > VALUES('$id',".$columns.")"); > $result2 = $dbh->execute($sth, $values); > > // ERROR > if(PEAR::isError($result2)){ > echo $table." insert failed:\n\n"; > dbError($result2); > } // end ERROR > } // end else > } // end function 'insertOrUpdate()' > > # > # Table structure for table guide_dining_meals > # > > CREATE TABLE guide_dining_meals ( > id int(11) NOT NULL default '0', > breakfast tinyint(4) NOT NULL default '0', > lunch tinyint(4) NOT NULL default '0', > dinner tinyint(4) NOT NULL default '0', > PRIMARY KEY (id) > ) TYPE=MyISAM; > > > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.1 (Darwin) > > iD8DBQE9//pmmU+jltT1nOoRAm9oAKCUFJSVjKPHr0hfEeO8oWT+4StHegCfd/tA > zQTnj5RQuCQAFnn2xioQjkQ= > =ODns > -----END PGP SIGNATURE----- > > > -- > PEAR General Mailing List (http://pear.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.pear.general (#3032) next »