Hi all,

I have a page on a site where a users image is shown. But if the user hasn't submitted any image a default image based on the users sex is shown. I made a simple PHP script for that in a block, but I get these wierd error messages because of it.

Whats wring with my code?

Code:

<?php
global $user; 
$uid = $user->uid;

if (db_query("SELECT fid FROM {file_managed} WHERE uid='$uid'")->fetchField()) {
// Show user img bla bla
}
else {
	$pid = db_query("SELECT pid FROM {profile} WHERE type='personal' AND uid=$uid")->fetchField();
	$sex = db_query("SELECT field_sex_value FROM {field_data_field_sex} WHERE bundle='personal' AND entity_id=$pid")->fetchField();
	echo '<img src="/images/sex'.$sex.'.png" alt="Defaultimage - '.$sex.'" />';
	echo '<div class="add-img-link"><a href="/drupal/profile-bild/'.$uid.'/edit/">Add image</a></div>';
}
?>

Error message:

Notice: Undefined index: args i DatabaseLog->findCaller() (row 155 of /drupal/includes/database/log.inc).

On the page where the user has submitted an image, only one error of these is shown. On Else, there are three of these error messages. They all say the same, which seem to be because of my dq_query, right?

But then what?

Comments

nevets’s picture

Does

db_query('SELECT fid FROM {file_managed} WHERE uid=:uid', array(':uid' => $uid))->fetchField()

work?

d.holmen@gmail.com’s picture

Thanks, but it gives the same error.

nevets’s picture

Just using the one db call I can not reproduce the error. I would suggest building the code up line by line till you know which one is causing the error.

Joe Vallender’s picture

I have come across while developing on d7.

I guess the db log can't find the calling function in eval'd code. If it is called from a function in a php/module/template file it is fine though

I put most of my code in custom panels, but I'd suggest you put your custom code in block hooks within a module.

http://api.drupal.org/api/drupal/modules--block--block.api.php/function/...
http://api.drupal.org/api/drupal/modules--block--block.api.php/function/...

I think keeping code in files and not in the db is a fairly good rule of thumb

salvis’s picture

You may have Devel's query logging enabled, which can be affected by a core bug that produces these notices.

The D8 core patch in #1739808-2: Notice: Undefined index: file in Drupal\Core\Database\Log->findCaller() may need to be ported to D7 to fix this, once it's accepted into D8.