i try to calculate the default value of my node reference the way i put above - the problem is that is always saying:
user warning: Unknown column 'perfil' in 'where clause' query: SELECT * FROM node WHERE type = perfil AND uid=1;
i know that perfil should go like this 'perfil' (manually SQL works); but even changing " to ' and putting '%s' makes no difference! any clue how to modify my sql statement? thanks in advance

global $user;
$perfiluser=$user->uid;
$perfil=perfil;
$result = db_query('SELECT * FROM {node} WHERE type = %s AND uid=%d', $perfil ,$perfiluser);
return array(
0 => array('nid' =>$result),
// You'll usually want to stop here. Provide more values
// if you want your 'default value' to be multi-value
);

Comments

mschneider’s picture

Assigned: mschneider » Unassigned
yched’s picture

Status: Active » Fixed

$perfil=perfil; should be $perfil='perfil';
WHERE type = %s should be WHERE type = '%s'

Also, you don't want SELECT *, but only SELECT uid

mschneider’s picture

thanks for you answer - changed it to

global $user;
$perfiluser=$user->uid;
$perfil='perfil';
$result = db_query('SELECT uid FROM {node} WHERE type = '%s' AND uid=%d', $perfil ,$perfiluser);

return array(
0 => array('nid' =>$result),
// You'll usually want to stop here. Provide more values
// if you want your 'default value' to be multi-value
);

but now the validation of drupal will block the thing. here is the message:

* user warning: Unknown column 'perfil' in 'where clause' query: SELECT * FROM node WHERE (type = perfil) AND (uid=1) in /home/....../content/sites/all/modules/cck/content.module(2015) : eval()'d code on line 4.
* The default value PHP code returned an incorrect value.
Expected format:

return array(
0 => array('nid' => value for nid),
// You'll usually want to stop here. Provide more values
// if you want your 'default value' to be multi-valued:
1 => array('nid' => value for nid),
2 => ...
);

Returned value:

even changing the syntax to ""
$result = db_query("SELECT uid FROM {node} WHERE type = '%s' AND uid=%d", $perfil ,$perfiluser);

mschneider’s picture

Status: Fixed » Active
yched’s picture

Status: Active » Fixed

Try with :

$result = db_result(db_query("SELECT nid FROM {node} WHERE type = '%s' AND uid = %d", $perfil ,$perfiluser));

- double quotes around the query are indeed needed (since '%s' introduces simple quotes in the string)
- db_query returns a db ressource id, not actual results, you need to use db_fetch_array(), or db_result() for a single result like above
- you want to fetch the nid, not the uid, my bad
- You'll need to decide what happens if several nodes match the conditions. The code above will pick one 'randomly' (SQL standards don't warrant results order if you don't specify an ORDER BY clause.

This is PHP / drupal API / MySQL support now, nothing specifically related to CCK :-)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.