1
$otvet = db_query("
SELECT {node}.title, {content_type_answer}.field_question_nid, {content_type_answer}.nid
FROM {node}, {content_type_answer}
WHERE {node}.nid = {content_type_answer}.field_question_nid
LIMIT %d, %d", 0, 3);
while($ref = db_fetch_object($otvet)) {
   $otvets[] = array($ref->field_question_nid, $ref->nid, $ref->title);
}
print '<pre>'; print_r($otvets); print '</pre>';

Is there a mistake after WHERE... ?

{content_type_answer}.field_question_nid has the same id as {node}.nid, and i have these nodes in my database. Why it doesnt works? I need to select {node}.title, {content_type_answer}.field_question_nid, {content_type_answer}.nid with condition that {node}.nid == {content_type_answer}.field_question_nid

1
  • Could you expand your question slightly? What is stored in field_question_nid? What EXACTLY do you want to extract from the db? Original node title? Node title of what I can only assume is the answer to a question?
    – Chapabu
    Dec 22, 2011 at 14:12

2 Answers 2

4
$otvet = db_query("
    SELECT n.title AS title, cta.field_question_nid AS nid
    FROM {node} n
    INNER JOIN {content_type_answer} AS cta ON cta.nid = n.nid
    LIMIT 0, 3
    ");

while($ref = db_fetch_object($otvet)) {
   $otvets[] = array($ref->nid, $ref->title);
}

print '<pre>'; 
print_r($otvets); 
print '</pre>';

Surely something like this would be a better option?

Your query also has the data for 3 placeholders, but it only actually has two placeholders in it. You have %d, %d as limit placeholders, but there is no '%s' to place the 'article' anywhere. This means that your query is going to try and place 'article' when it's expecting an integer (which will make it fall over).

EDIT

Changed query as per comment.

12
  • i've noticed that i make mistake with a empty argument 'answer'. Now is the question, when im printing node title, it shows title from {cta}.field_question_nid, how to get title from {cta}.nid ? Dec 22, 2011 at 12:32
  • If I understand correctly, you just need to change the inner join to cta.nid instead of cta.field_question_nid.
    – Chapabu
    Dec 22, 2011 at 12:40
  • Could u throw an example? Dec 22, 2011 at 12:49
  • FROM {node} n - what does "n" means - can you explain please? Dec 22, 2011 at 12:51
  • Having {node} n is the same as typing {node} AS n - it declares an n as an alias of {node} (w3schools.com/sql/sql_alias.asp - reference) Basically it means that {node} can thereafter be referenced as n.
    – Chapabu
    Dec 22, 2011 at 12:52
1
$otvet = db_query("
  SELECT node.title, content_type_answer.field_question_nid, content_type_answer.nid
  FROM {node}, {content_type_answer}
  WHERE node.nid = content_type_answer.field_question_nid
  LIMIT %d, %d", 'answer', 0, 3);
while($ref = db_fetch_object($otvet)) {
   $otvets[] = array($ref->field_question_nid, $ref->nid, $ref->title);
}
print '<pre>'; print_r($otvets); print '</pre>';

Install Devel to replate print '<pre>'; print_r($otvets); print '</pre>'; by dpm($otvets); and debug easily

2
  • Thanks, i know about devel dpm(), but im not printing $messages on the fron page of site. I didn't noticed any difference between mine and yours code O_o Dec 22, 2011 at 10:48
  • The {} is only for from for SELECT and WHERE it use the alias that will be be the same if you write SELECT node.title, content_type_answer.field_question_nid, content_type_answer.nid FROM {node} node, {content_type_answer} content_type_answer WHERE node.nid = content_type_answer.field_question_nid
    – nourcy
    Dec 22, 2011 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.