gpt4 book ai didi

drupal-7 - Drupal 7 : fastest way to check if db entry exists

转载 作者:行者123 更新时间:2023-12-04 13:24:05 25 4
gpt4 key购买 nike

如果数据库条目存在,我怎样才能最快地检查?我使用这个代码:

$exists = db_query('SELECT tid FROM {taxonomy_index} WHERE tid = 1 AND nid = 1 LIMIT 1');
if($exists->rowCount() > 0){
drupal_set_message("exists");
}

最佳答案

我会做:

$result = db_select('taxonomy_index', 'ti')
->fields('ti', array('tid'))
->condition('tid', 1)
->condition('nid', 1)
->range(0, 1)
->execute()
->rowCount();

if ($result) {
drupal_set_message(t('Exists'));
}

与您的问题无关,但您应该始终使用占位符来防止 SQL 注入(inject) - 尽管如果您使用上面的查询构建器,那么它会为您处理。此外,在将文本写入屏幕时,您应该始终使用 t() 函数。

关于drupal-7 - Drupal 7 : fastest way to check if db entry exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11172539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com