SQL Query function and joining databases - php

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%' ;

Related

How to optimize nested query with PHP and MySqli?

I have this code, that fetches data from two tables in MySQLi.
It first has to get the title, description, status and project_id from table 1, and then get the name from table 2 using the id from table 1.
Is there a better/faster way to do this? I have about 600 rows in the tables, and it takes about 5 sec to run this query. I will also add that this example is a bit simplified, so please don't comment on the db-structure.
<?php
$results = $connect()->db_connection->query(
'SELECT title, description, status, project_id
FROM table
WHERE created_by ='.$user_id
);
if ($results) {
while ($result = $results->fetch_object()) {
$res = $connect()->db_connection->query(
"SELECT name FROM projects WHERE id = ".$result->project_id
);
if ($res) {
while ($r = $res->fetch_object()) {
echo $r->name;
}
}
echo $result->title;
echo $result->status;
}
}
?>
Use Query:
SELECT title,description,status,project_id
FROM table tb
inner join projects pr on pr.id = tb.project_id
WHERE created_by = $user_id
Try to use JOIN in your query.
You can find examples and description of this command here: http://www.w3schools.com/sql/sql_join.asp
Check out also this infographics:
http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg
You can use JOIN on project_id:
$results = $connect()->db_connection->query('SELECT t.title title,t.description,t.status status,t.project_id, p.name name FROM `table` t JOIN projects p ON p.id= t.project_id WHERE t.created_by ='.$user_id);
if($results){
while($result = $results->fetch_object()){
echo $result->name;
echo $result->title;
echo $result->status;
}
}
tables have aliases here - t for table and p for projects.
Also to make it faster, add index to project_id in table table, if you haven't done it yet:
$connect()->db_connection->query('ALTER TABLE `table` ADD INDEX `product_id`');

Merging these two database select queries

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

SQL function for data retrieval JOIN (second attempt)

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

PHP take id's from one table and pull data that corresponds with those id's from another table

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

PHP, SQL newbie help

I have the following table:
Comments
--------
id PK
cid
content
uid
comment
If the content is image /i want it to print ?imgID=$cid and get the data from the row title from the table images and if it's a thread I want it to print ?threadID=$cid and get the title from the table threads and so on. How should I do this?
<h3>Latest comments</h3>
<?php
$rs = mysql_query("
SELECT *
FROM comments
LEFT JOIN threads
ON threads.id = comments.cid
WHERE comments.uid = $id
");
while ($row = mysql_fetch_assoc($rs)) {
echo $row['title'];
}
while ($row = mysql_fetch_assoc($rs)) {
if($row['content'] == "image") {
echo "?imgID={$cid}";
$title = mysql_fetch_assoc(mysql_query("SELECT title from images WHERE id = '$cid'");
} elseif ($row['content'] == "thread") {
echo "?threadID={$cid}";
$title = $row['title']; // Will only work as long as there is no title field in comments, oherwise use threads.title
}
}
echo $title;
There you go. The curly brackets around the $cid aren't strictly nessecary, but they help avoid issues where if you are trying to print text afterwards, and it reads the variable name as something else.
$q="SELECT c.id, c.cid, c.content, c.uid, c.comment,
COALESCE(t.title,i.title) AS the_title
FROM comments c LEFT JOIN threads t ON (c.cid=t.id AND c.content='thread')
LEFT JOIN images i ON (c.cid=i.id AND c.content='image');
The above combines your two queries together and puts the title attribute in one column.

Categories