June 29th, 2000, 04:54 PM
Is there a better way to check and see if a certain value has already been inserted into a primary key field besides just doing a select * from .. where .. and seeing if it returns any rows before inserting the new record?
[This message has been edited by RyanP (edited June 29, 2000).]
June 30th, 2000, 04:25 AM
The only better way is to not have to check the primary key. If you use NOT NULL AUTO_INCREMENT then the primary key is automatically incremented every time you do an INSERT.
CREATE TABLE sometable ( sid INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY (sid)
Then the key is automatically incremented when inserted into. i.e.
$query = "INSERT INTO sometable VALUES (NULL, '$text1val', '$text2val', $num1, now() )";
After executing the query the key is inserted automatically when it detects the NULL.
Then if you need to know the primary key value. In PHP you would use:
$primaryKey = mysql_insert_id($mysql_result);
Other than that I don't know of a better way.