Combining multiple MYSQL queries into one with PHP - php

I know this subject has been covered before, and I've read about a dozen of the links provided by stackoverflow. None match my need.
I have 4 mysql queries using PHP for similar data, I'd like to lower that to one query and maybe put the results in an array that I can access. Here is my current code.
$id = $row[post_id];
$resulttwo = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'length' ");
$temptwo = mysql_fetch_array($resulttwo);
$length[$id] = $temptwo[0];
$id = $row[post_id];
$resultthree = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'location_city' ");
$tempthree = mysql_fetch_array($resultthree);
$trailcity[$id] = $tempthree[0];
$id = $row[post_id];
$resultfour = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'location_state' ");
$tempfour = mysql_fetch_array($resultfour);
$trailstate[$id] = $tempfour[0];
$id = $row[post_id];
unset($tempfour);
$resultfour = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'difficulty' ");
$tempfour = mysql_fetch_array($resultfour);
$difficulty[$id] = $tempfour[0].' difficulty';`

This should work:
$id = $row[post_id];
$result = mysql_query("SELECT meta_key, meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` IN ('length', 'location_city', 'location_state', 'difficulty')");
$temp = mysql_fetch_assoc($result);
Array $temp will contain the meta_key along with the meta_value which you should be able to call like so $temp[length]. You can check the entire array with print_r($temp);
You should also stop writing new code using mysql_ functions as they are being deprecated and use mysqli_ or PDO instead.

This should be sufficient as you only care about the first row in each query.
SELECT meta_value FROM wp_postmeta WHERE post_id = $id AND meta_key IN ("length", "location_city", "location_state", "difficulty") LIMIT 4;
Damn too late! :/

Related

PHP/SQL, Reduce number of queries

I am querying a large db in php, and I am not doing it near well enough.
I have the code I need, I simple cannot find a good way to compact these statements.
$q = $_GET['q'];
(dbinit)
$query = "SELECT post_id FROM wp_postmeta WHERE meta_key = '_billing_email' AND meta_value = '$q'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
$thing = $row['post_id'];
$querys = "SELECT 'meta_value' FROM wp_postmeta WHERE post_id = '$thing' AND meta_key = '_order_number'";
$results = mysql_query($querys);
$rows = mysql_fetch_array($results);
$postid = $rows['meta_value'];
$queryss = "SELECT post_id FROM $table WHERE meta_value = $postid AND meta_key = '_order_number'";
$resultss = mysql_query($query);
$rowss = mysql_fetch_array($result);
$order_id = $rowss['post_id'];
(handling)
}
I am wondering if there is a more efficient way to do these queries, or perhaps have them in one query?
Not sure where $table comes from.
But this JOIN attempt should work:
$q = $_GET['q'];
// ... dbinit ...
$query = "SELECT wpp.post_id,
wpp1.meta_value as orderNumber,
wpp2.post_id as orderId
FROM wp_postmeta wpp
LEFT JOIN wp_postmeta wpp1
ON wpp1.post_id = wpp.post_id
AND wpp1.meta_key = '_order_number'
LEFT JOIN $table wpp2
ON wpp2.meta_value = wpp1.meta_value
AND wpp2.meta_key = '_order_number'
WHERE wpp.meta_key = '_billing_email'
AND meta_value = '$q'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
(handling)
}
As you can see you can get
$thing = $row['post_id'];
$order_id = $row['orderId'];
inside the loop if needed.

How would I get values from 2 rows into one

I need to access data from 2 different rows returned in the same array
Here is a picture
I need to access first_name and last_name
Here is my script
<?php
ini_set('display_errors', 1);
include_once('db.php'); // database class
$dbase['host'] = 'asdf';
$dbase['user'] = 'asdf';
$dbase['pass'] = 'asdf';
$dbase['name'] = 'asdf';
$db = new DB($dbase);
// SELECT `meta_value` FROM `wp_usermeta` WHERE `meta_key`='password'
$result = $db->query("SELECT * FROM `wp_usermeta` WHERE `meta_key`='user_email'");
while ($row = $result->fetch_array()) {
echo $row['meta_value'];
}
?>
Any help on this issue would be appreciated greatly!
Try this query..
SELECT
wp1.meta_value AS first_name,
wp2.meta_value AS last_name
FROM
wp_usermeta wp1
INNER JOIN
wp_usermeta wp2
ON ( wp1.user_id = wp2.user_id )
WHERE 1
AND wp1.meta_key = "first_name"
AND wp2.meta_key = "last_name";
GROUP BY wp1.user_id;
You're already looping over these rows, just inspect the meta_key.
$fname="";
$lname="";
while ($row = $result->fetch_array())
{
if ($row['meta_key'] == "first_name")
{
$fname = $row['meta_value'];
}
else if ($row['meta_key'] == "last_name")
{
$lname = $row['meta_value'];
}
}
echo $fname . " " . $lname;
lastly, your sql is incorrect:
SELECT * FROM `wp_usermeta` WHERE `user_id` IN (SELECT `user_id` FROM `wp_usermeta` WHERE `meta_key` = 'user_email' AND `meta_value` = "$user_email_passed_in")
In this case the MySQL query that you're doing is wrong.
It might be
$result = $db->query("SELECT * FROM `wp_usermeta` WHERE (`meta_key`='first_name' OR `meta_key`='last_name')");
In this case all rows matching 'first_name' OR 'last_name' in 'meta_key' field will be returned.
I think you'll have to distinguish these fields using also user_id field as discriminant.
Best regards

select query in wordpress

I am trying to do this by 2 hours.
I have custom fields in database and I want to get post_id by the meta keys or meta values.
I am doing like this
$post_id = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
print_r($post_id); // giving only 140
this is working fine, but this is giving only one post_id and I want all possible post_id matched by meta_value.
for example :
I have three post 140,141,142, in database. But by this query I am only getting 140.
Any Idea how to get all possible post_id by this query or any other way by comparing meta_fields...
Thanks
$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
print_r($post_id); /
$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
print_r($post_id);
for more information ... http://codex.wordpress.org/Class_Reference/wpdb
<?php
$user_id = 1;
global $wpdb;
$wpdb_prefix = $wpdb->prefix;
$wpdb_tablename = $wpdb_prefix.'Table_Name';
$result = $wpdb->get_results(sprintf('SELECT `colum1`, `colum2` FROM `%2$s` WHERE `user_id` = %d LIMIT 1', $user_id, $wpdb_tablename));
print_r($result); exit;
?>

Is there a more efficient way to write these 4 MYSQL Queries to use fewer queries?

My current queries. Can I combine them in to fewer to get the same result? I am trying to get specific information about the trail into an array named $trail.
$result = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'location_city' ");
$result = mysql_fetch_array($result);
$trail[city] = $result[0];
$result = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'length' ");
$result = mysql_fetch_array($result);
$trail[length] = $result[0];
$result = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'location_state' ");
$result = mysql_fetch_array($result);
$trail[state] = $result[0];
$result = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'difficulty' ");
$result = mysql_fetch_array($result);
$trail[difficulty] = $result[0];
$result = mysql_query("SELECT guid FROM wp_posts WHERE `post_parent` = $id AND `post_type` = 'attachment' ");
$result = mysql_fetch_array($result);
$trail[image] = $result[0];
This will work for the meta_values:
SELECT meta_value, meta_key
...
WHERE (post_id = $id) AND (meta_key IN ('location_city', 'length', ...)
The guid query will probably be better off run as a separate one. If you insist on reducing to a single query call, then a union would do
SELECT 'meta' AS source, meta_value, meta_key
WHERE ...
UNION ALL
SELECT 'guid' AS source, guid, null
FROM ...
WHERE post_parent = $id and post_type = 'attachment'
then use this derived 'source' field to figure out which value(s) belong where.
If you want the data in columns, then you could JOIN the queries:
SELECT lc.meta_value Location_City,
l.meta_value Location,
ls.meta_value LocationState,
d.meta_value Difficulty,
p.guid
FROM wp_postmeta lc
LEFT JOIN wp_postmeta l
on lc.`post_id` = l.`post_id`
and l.`meta_key` = 'length'
LEFT JOIN wp_postmeta ls
on lc.`post_id` = ls.`post_id`
and ls.`meta_key` = 'location_state'
LEFT JOIN wp_postmeta d
on lc.`post_id` = d.`post_id`
and d.`meta_key` = 'difficulty'
LEFT JOIN wp_posts p
on lc.`post_id` = p.`post_parent`
and p.`post_type` = 'attachment'
WHERE lc.`post_id` = $id
AND lc.`meta_key` = 'location_city'
SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND (`meta_key` = 'difficulty' OR `meta_key` = 'location_state' OR `meta_key` = 'length' OR `meta_key` = 'location_city' )

Is it possible to combine these 3 mySQL queries?

I know the $downloadfile - and I want the $user_id. By trial and error I found that this does what I want. But it's 3 separate queries and 3 while loops. I have a feeling there is a better way. And yes, I only have a very little idea about what I'm doing :)
$result = pod_query("SELECT ID FROM wp_posts WHERE guid LIKE '%/$downloadfile'");
while ($row = mysql_fetch_assoc($result)) {
$attachment = $row['ID']; }
$result = pod_query("SELECT pod_id FROM wp_pods_rel WHERE tbl_row_id = '$attachment'");
while ($row = mysql_fetch_assoc($result)) {
$pod_id = $row['pod_id']; }
$result = pod_query("SELECT tbl_row_id FROM wp_pods_rel WHERE tbl_row_id = '$pod_id' AND field_id = '28'");
while ($row = mysql_fetch_assoc($result)) {
$user_id = $row['tbl_row_id']; }
Assuming I am understanding your queries correctly, this should work:
SELECT wp.ID, wpr.pod_id, wpr.tbl_row_id
FROM wp_pods_rel AS wpr
JOIN wp_posts AS wp
ON wp.ID = wpr.tbl_row_id
WHERE wpr.field_id = '28'
AND wp.guid LIKE '%/$downloadfile'
SELECT wp_posts.ID, wp_pods_rel.pod_id, wp_pods_rel.tbl_row_id
FROM wp_posts
JOIN wp_pods_rel ON wp_posts.ID = wp_pods_rel.tbl_row_id
WHERE wp_posts.guid LIKE '%/$downloadfile' AND wp_pods_rel.field_id = '28'

Categories