简体   繁体   中英

Not able to display one item from query result - PHP/MySQL

So, weird issue.

Here's the code:

//connect and execute query
mysql_connect('127.0.0.1','user','password');
@mysql_select_db('db_name') or die( "Unable to select database");

$query="SELECT node_revisions.nid, node_revisions.title, content_type_training_event.field_course_url_value
    FROM node_revisions, content_type_training_event
    WHERE body LIKE '%{$term}%' AND node_revisions.nid IN (SELECT nid FROM content_type_training_event) AND content_type_training_event.nid = node_revisions.nid";

$result=mysql_query($query);

$num = mysql_numrows($result);  

if ($num > 0)
{
while ($col = mysql_fetch_assoc($result)) 
{       

$nid=trim($col["nid"]);
$title=trim($col["title"]);
$url=trim($col["field_course_url_value"]);

}
}

//OK weird part...

echo($nid . $title . $url);

So, what happens is $title and $url are printed to screen, but $nid is not. When I run the same query inside MySQL Admin, I get the nid.

Any ideas? This is driving me nuts.

Maybe because there is an NID in multiple tables. Alias the NID in your query as follows:

$query="SELECT node_revisions.nid as nid, node_revisions.title, content_type_training_event.field_course_url_value
    FROM node_revisions, content_type_training_event
    WHERE body LIKE '%{$term}%' AND node_revisions.nid IN (SELECT nid FROM content_type_training_event) AND content_type_training_event.nid = node_revisions.nid"

I certainly could be wrong; feel free to downvote :)

Try

SELECT node_revisions.nid as currentNid, node.....

And then

$nid=trim($col["currentNid"]);
$title=trim($col["title"]);
$url=trim($col["field_course_url_value"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM