欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to replace db_result in d7 有大用 有大大用

How to replace db_result in D7

I need a simple code to check if a record exists in a db table. I found this code which does what I want though db_result no longer exists in D7.
$exists = db_result(db_query('SELECT 1 FROM {artistfield} WHERE uid = %d', $user->uid));
How would I write this statement in D7?

Comments

Web Assistant’s picture                                        

                                       

Just getting used to D7 myself, does something like the below work?

$result = db_query("SELECT 1 FROM {artistfield} WHERE uid = :uid", array(':uid' => $user->uid))->fetchObject();
                                                   
jordojuice’s picture                                            

                                           

Yeah I don't think there's an equivalent of db_result(). That's the closest I've found, so note that you will get an object, which means you need to check it with $result->1 or similar instead of just $result.

Web Assistant’s picture                                                

                                               

How about something like this...

<?php
$result = db_query("SELECT 1 FROM {artistfield} WHERE uid = :uid", array(':uid' => $user->uid))->fetchField();
?>
                                                               

Got that from here..

http://api.drupal.org/api/drupal/includes--database.pgsql.inc/function/d...                                                                

jordojuice’s picture                                                    

                                                   

Oooooh now I think I remember seeing that before too. Thanks.

Firehawk777’s picture                                                        

                                                       

That was just what I was looking for! All works great now!
Thanks guys!!!

only4kaustav’s picture                                        

                                       

If you are working in a migration project from d6 to d7 and if there are multiple occurrence of db_result in your custom module you can simply write the below function in any of your custom module without changing the same.

function db_result($res){
	return $res->fetchField();
                                                   

来自   https://www.drupal.org/forum/support/module-development-and-code-questions/2011-08-21/how-to-replace-db_result-in-d7

     



PHP Fatal error: Call to undefined function db_result() in adsense_search.install on line 14

PHP Fatal error: Call to undefined function db_result() in adsense_search.install on line 14

db_result is not available for Drupal 7, I think.

Pantheon is proud to support Drupal and open sourcePantheon logo

Comments

SocialNicheGuru created an issue. See original summary.

tashaharrison80’s picture

Simple fix, replacement for db_result.

$sql = db_query("SELECT name FROM {variable} WHERE name LIKE 'adsense_search__%'",
    array('adsense_search_%'))->fetchCol();
  $count = count($sql);
  if ($count != 0) {
    for ($i = 0; $i <= $count; $i++) {
       variable_del($sql[$i]);
    }
  }
SocialNicheGuru’s picture

Status:Active» Needs review

来自  https://www.drupal.org/project/adsense_search/issues/2560639



普通分类: