Trying to write an SQL function that collects data from two different tables. It should get the current post id from wp_posts and then find the corresponding images from a table called wp_postdata.
Table 1
------------------------------
id || title || content
------------------------------
Table 2
---------------------------------------------------------
meta_id || post_id || meta_key || meta_value
---------------------------------------------------------
The meta_key that we are after is called _wp_attachment_metadata
My failed attempt ::
<?php
$pics = mysql_query("SELECT id,
meta_key_wp_attached_file.meta_value picture
FROM wp_posts
LEFT JOIN wp_postmeta meta_key_wp_attached_file
ON meta_key_wp_attached_file.post_id = wp_posts.id
WHERE meta_key_wp_attached_file.meta_key = 'picture'
ORDER BY picture;");
while ($row = mysql_fetch_array($pics)) {
$thumb = $row['picture'];
echo '<img src="http://www.golfbrowser.com/wp-content/uploads/'.$thumb.'" />';
}
?>
Any ideas,
Marvellous
Should be: meta_key = '_wp_attachment_metadata', and you'll need to unserialize the result.
Or you can use the API:
http://codex.wordpress.org/Function_Reference/WP_Query
http://codex.wordpress.org/Function_Reference/get_post_meta
Related
I have two table records in my database which look like this:
Table 1 with the column 1:
topic_id name
21 my computer
table 2 with columns as follows:
reply_id topic_id message
1 21 blabla
2 21 blue
In which the topic_id column in the table 2 is the foreign key of the table 1
I wanted to echo all replies in the table 2 along with the topic name (#21) in the table 1. So, I made the query like this
$q="SELECT name, message
FROM table1
LEFT JOIN table2
ON table1.topic_id = table2.topic_id
";
However, the result/ output returns the topic's name and ONLY ONE reply, but not 2 (or all) as expected. Did I miss something?
I used LEFT JOIN because some topics are still waiting for replies. In case that there is not any reply, the topic's name is still printed in browsers.
I also tried adding
GROUP BY table1.topic_id
but still NO LUCK!
Can you help? Thanks
EDIT: To clarify the question I add the php code to fetch records as follows:
As you know, The name needs to be printed only once. So, I code like this:
$tid = FALSE;
if(isset($_GET['qid']) && filter_var($_GET['qid'], FILTER_VALIDATE_INT, array('min_range'=>1) ) ){
// create the shorthand of the question ID:
$tid = $_GET['tid'];
// run query ($q) as shown above
$r = mysqli_query($dbc, $q) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $q");
if (!(mysqli_num_rows($r) > 0) ){
$tid = FALSE; // valid topic id
}
}//isset($_GET['qid']
if ($tid) { //OK
$printtopic = FALSE; // flag variable to print topic once
while($content = mysqli_fetch_array($r, MYSQLI_ASSOC)){
if (!$printtopic) {
echo $content['name'];
$printtopic= TRUE;
}
}
} // end of $tid
// Print the messages if any:
echo $content['message'];
Try this with inner join
$q="SELECT name, message
FROM table1
INNER JOIN table2
ON table1.topic_id = table2.topic_id";
Try:
$q="SELECT table2.reply_id, table1.name, table2.message
FROM table2
LEFT JOIN table1
ON table1.topic_id = table2.topic_id
";
After struggling with this issue, I can find out that the problem is that I had to change the query to INNER JOIN and add the WHERE clause like this:
WHERE table2.reply_id = {the given topic_id}
Then it works well!
Sorry to disturb you all!
I'm creating a image gallery where people can add comments to each uploaded image.
I've made a db tabel for all comments including a foreign key to match the image table.
My SQL variable looks like this and seems to work fine:
$all_comments_one_image_sql="
SELECT userimage.id, image_id, comment, id, comment_date
FROM userimage, image_comment
WHERE image_comment.image_id=userimage.id
ORDER BY comment_id DESC
";
But when I'm doing my while function for all my gallery images, where and how would I echo the right comments to each image?
You should be doing a left join like this:
select userimage.id,
com.image_id,
com.comment,
com.id,
com.comment_date
FROM userimage
LEFT JOIN image_comment as com ON com.image_id=userimage.id
WHERE userimage.id = ?
ORDER BY com.id DESC
First of all create a function for getting image comments:
function getImageComments($imageId){
$query = "SELECT userimage.id, image_id, comment, id, comment_date
FROM userimage
LEFT JOIN image_comment
ON image_comment.image_id=userimage.id
WHERE image_id = $imageId
ORDER BY comment_id DESC
";
mysql_query($query);
return mysql_fetch....
}
I also redo your query for better performance.
So whnever you are loading an image, call the function with the proper image id and get its comments.
You have to check for the right image_id
$all_comments_one_image_sql = "
SELECT userimage.id, image_id, comment, id, comment_date
FROM userimage, image_comment
WHERE image_comment.image_id=userimage.id AND image_id = ?
ORDER BY comment_id DESC
";
where ? is the id of the image you want to Show comments - if you use pdo, you can just bind the image id and don't have to care about escaping.
If you have a summary page for all images, you can add the results in an assoc array, where the key is your image id
$comments[] = array();
while ($row = $res->fetch()) {
if (empty($comments[$row->image_id])) {
$comments[$row->image_id] = array();
}
$comments[$row->image_id][] = $row;
}
To show the comments, you can just look, if $comments[$image_id] is an array and show the conent
I wonder if there is a way of merging these two database queries so as to have one:
$result = $wpdb->get_row("SELECT meta_value FROM ".$wpdb->prefix.
"postmeta WHERE meta_key = '_cat_num' AND post_id = $var");
$name = $wpdb->get_row("SELECT name FROM ".AH_FEED_DETAILS_TABLE.
" WHERE id = " . (int)$result->meta_value);
return $name->name;
The first query finds the category value which is then used to find the name field in the AH_FEED_DETAILS_TABLE table
You can get the database to do the work for this by using the IN with a sub-query as follows:
$name = $wpdb->get_row("SELECT name FROM ".AH_FEED_DETAILS_TABLE.
" WHERE id IN (SELECT meta_value FROM ".$wpdb->prefix.
"postmeta WHERE meta_key = '_cat_num' AND post_id = $var)");
$result = $wpdb->get_row("SELECT name FROM ".$wpdb->prefix
.AH_FEED_DETAILS_TABLE." AS cat_tbl JOIN postmeta ON
cat_tbl.id=postmeta.meta_value
WHERE postmeta.meta_key = '_cat_num' AND postmeta.post_id = $var");
return $result->name;
I hope that helps giving the idea
I would like to get all the the corresponding images from one mySQL table that have a corresponding post_ID to the ID of another table.
So with an ID of 7 in table wp_post, I want to echo the values of infomation in another table that has a certain meta_key and a corresponding ID.
===WP_POST=========== ===WP_POSTMETA======================================
ID || content post_id || meta_key || meta_value
================================================================================
The meta_key that I want is listed as _wp_attached_file in the database.
So I want to get all meta_values of wp_postmeta table that have a meta_key of _wp_attached_file and a post_id that matches the current ID of the page being viewed from the table wp_posts.
So obviously the tables will need to be joined but I need a hand.
Any ideas,
Marvellous AHAA
Implemented solution below and I am now receiving an error.
<?php
$pics = mysql_query("SELECT t2.meta_key,t2.meta_value from (select id from wp_post) t1
join wp_postmeta t2 on (t2.post_id=t1.postid)
where t2.meta_key='_wp_attached_file'");
while ($row = mysql_fetch_array($pics)) {
$thumb = $row['meta_value'];
echo '<img src="http://www.golfbrowser.com/wp-content/uploads/'.$thumb.'" />';
}
?>
Any ideas?
I tried a simpler way myself. Still does not work.Can anyone see what is wrong.
<?php
$post_id = $post->ID;
$pics = mysql_query("
SELECT meta_value * FROM wp_postdata
WHERE meta_key ='_wp_attached_file'
AND post_id = '%$post_id%'
");
while ($row = mysql_fetch_array($pics)) {
$thumb = $row['meta_value'];
echo '<img src="http://www.golfbrowser.com/wp-content/uploads/'.$thumb.'" />';
}
?>
Try this query:
SELECT A.meta_key
FROM WP_POSTMETA AS A
JOIN WP_POST AS B
ON (A.post_id = B.ID)
WHERE A.post_id='%post_id%' ;
I have this query:
$query = "SELECT *FROM wp_postmeta WHERE meta_key = '_isEvent' AND meta_value ='yes' ORDER BY post_id LIMIT 0, 5" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$eventid = $row ['post_id'];
echo "<p>".$eventid."</p>";
}
This currently posts some ids $eventid. I now want to run another query in another table (same database) that pulls some post titles that matches those id's.
Table is called "wp_posts" and column to match is "ID" and want to echo out post title from "post_title".
Where do I start?
Sounds like you want to use SQL joins. W3 schools has a good intro article about this.
http://www.w3schools.com/sql/sql_join.asp
Something like the following
SELECT post_title FROM wp_postmeta m, wp_posts p WHERE m.wmeta_key = '_isEvent' AND m.meta_value ='yes' AND m.post_id == p.post_idORDER BY m.post_id LIMIT 0, 5