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
https://forums.phpfreaks.com/topic/244806-needing-wordpress-database-query-help/
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.

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. 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.