database query for getting nid for checking duplicate title in Drupal 6.x
I would like to write SQL query for finding the node ID (nid) for which title matches a given title (value of title field value) for a give content type. I am trying the following-
function title_ajax_check_duplicate($title, $type) {
$results = db_query("SELECT nid FROM {node} WHERE title = '%s' AND type = '%s'", $title, $type);
if (!empty($results)) {
// This is a duplicate.
return $results;
}
else {
return FALSE;
}
}
I am not able to solve this for a long time. Any help would be much appriciated. Thanks in advanc开发者_JAVA百科e.
$results will return a mysql resource regardless of actual results I think.
Checkout db_fetch_array this will actually contain the results. You'd then have to iterate over the array of results to find the duplicates.
Change you query line to:
$result = db_result(db_query("SELECT nid FROM {node} WHERE title = '%s' AND type = '%s'", $title, $type));
精彩评论