|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
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).] |
|
#2
|
|||
|
|||
|
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.
i.e. CREATE TABLE sometable ( sid INTEGER NOT NULL AUTO_INCREMENT, text1 VARCHAR(255), text2 VARCHAR(255), number1 INTEGER, date1 DATE, 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. |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Checking to see if an identical value for a primary key field already exists |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|