Jump to content

Needing WordPress database query help...


Jim R

Recommended Posts

Let me start by saying this is my attempt at coding with anything involving a Join, on my own at least.  It's failed.  : )

 

I'm trying to match the Tags of Images so they show up on the related Tag Archive page (mostly basketball players for me).  I know the first line works and brings the tag_id in so it can be used. 

 

The datatable where the images are stored is wp_ngg_pictures.  Logically it goes like this:

 

np.pid (128,140,147) = tr.object_id (128,140,147)

tr.term_taxonomy_id (1200) = tt.term_taxonomy_id (1200)

tt.term_id (where taxonomy = ngg_tag) (517) = t.term_id (where t.term_id = $wp_tagID) (517)

 

 


$wp_tagID = get_query_var('tag_id');  

$qImage = 'SELECT * FROM wp_ngg_pictures AS np
LEFT JOIN wp_term_relationships AS tr ON np.pid = tr.object_id
LEFT JOIN wp_term_taxonomy_id AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN wp_terms AS t ON tt.term_id = t.term_id
WHERE tt.taxonomy = ngg_tag
AND t.term_id = "$wp_tagID"	
';

$pImage = mysql_query($qImage);
while($images = mysql_fetch_assoc($pImage)) {        <-- line 109 <---

echo $images;
}


 

 

I'm getting this error:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/jwrbloom/public_html/resources/wp-playerProfile.php on line 109

Link to comment
Share on other sites

That error is caused by simply passing the result of mysql_query to mysql_fetch_assoc without firstly checking to see if your query actually succeeds or not.

 

I swear I post this at least a few times a day:

 

if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    // $result holds records
  } else {
    // no record found, handle error
  }
} else {
  // query failed, handle error
}

 

To give you a hint as to why your query is failing take a look at the output from mysql_error.

Link to comment
Share on other sites

I appreciate the link, but your code doesn't really help me.  It just changes the output when I get an error vs. when the query produces nothing.  In this instance it's likely both, and I'm really trying to work my way through Joins.  What I've read on them doesn't give me much to go on, and that includes searching here and reading MySQL. 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.