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'
Related
I created a SELECT to get my communities.
And create two SELECTs to get the communities I'm following.
But I get just my communities.
I do not get the communities I'm following.
$user_id = $_GET["id"];
$row1 = array();
$row2 = array();
// get my communities
$res1 = mysql_query("SELECT * FROM communities where user_id = '$user_id'");
while($r1 = mysql_fetch_assoc($res1)) {
$row1[] = $r1;
}
// get "id" of my communities I'm following
$res = mysql_query("SELECT * FROM communities_follow where user_id = '$user_id'");
while($r = mysql_fetch_assoc($res)) {
$coid = $r["coid"];
// get my communities I'm following
$res2 = mysql_query("SELECT * FROM communities where id = '$coid'");
while($r2 = mysql_fetch_assoc($res2)) {
$row2[] = $r2;
}
}
$resp = array_replace_recursive($row1, $row2);
print json_encode( $resp );
The inner join will get you those communities only, where you are following:
SELECT c.* FROM communities c
INNER JOIN communities_follow cf ON c.id = cf.coid
WHERE cf.user_id = '$user_id';
Or, without a JOIN:
SELECT * FROM communities
WHERE EXISTS (SELECT 1 FROM communities_follow cf
WHERE c.id = cf.coid AND cf.user_id = '$user_id')
Try this sql.
SELECT * FROM communities c LEFT JOIN communities_follow cf ON c.user_id = cf.user_id where
cf.user_id = '$user_id';
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.
I currently have this query with an array that outputs the variables within using a dynamic input in my form (term), this creates a Dynamic Search with auto complete to fill in all of the details for a product.
$return_arr = array();
$param = $_GET["term"];
$fetch = mysql_query("SELECT * FROM crd_jshopping_products WHERE `name_en-GB` REGEXP '^$param'");
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
//$row_array['category_id'] = $row ['category_id'];
$row_array['product_id'] = $row['product_id'];
$row_array['product_names'] = $row['name_en-GB'];
$row_array['jshop_code_prod'] = $row['product_ean'];
$row_array['_ext_price_html'] = number_format($row['product_price'],2);
if (!empty($row['product_thumb_image']) AND isset($row['product_thumb_image'])){
$row_array['image'] = $row['product_thumb_image'];
}else {
$row_array['image'] = 'noimage.gif';
}
array_push( $return_arr, $row_array);
}
mysql_close($conn);
echo json_encode($return_arr);
Unfortunately I also need to get the category_id which is not in the same table, I have tried to modify my query as such, but to no avail:
$fetch = mysql_query("SELECT * FROM crd_jshopping_products WHERE `name_en-GB` REGEXP '^$param' AND `crd_jshopping_products_to_categories` = `product_id` ");
What step am I missing here ? The product_id's match in both tables?
try this query instead and try to understand what I have written in it:
$fetch = mysql_query("
SELECT
p.*,
c.category_id
FROM
crd_jshopping_products as p
INNER JOIN crd_jshopping_products_to_categories as c
ON p.product_id = c.product_id
WHERE
`p.name_en-GB` REGEXP '^$param'
");
This means:
SELECT:
Give me everything from p and the category_id from c.
FROM:
Do this from rows in the tables crd_jshopping_products (referred to as p) and crd_jshopping_products_to_categories (referred to as c), where the rows match on the count of p.product_id is the same as c.product_id.
WHERE:
Only return the rows where p.name_en-GB REGEXP '^$param'.
pretty sure this is something easy but i cant figure it out, basicly making a easy query like this:
$result = $mysqli->query("SELECT t.name FROM cups_participants cp
LEFT JOIN teams t on cp.team_id = t.team_id
WHERE cp.cup_id = '1'");
the above query should get all the names out of the database, now i want it to be put into array like this:
$competitors = array(
'Paul A.M. Dirac',
'Hans Christian Oersted',
'Murray Gell-Mann',
'Marie Curie',
'Neils Bohr',
'Richard P. Feynman',
'Max Planck');
how do i get the result from my query into a array like the above on?
$competitors = array();
$result = $mysqli->query("SELECT t.name FROM cups_participants cp
LEFT JOIN teams t on cp.team_id = t.team_id
WHERE cp.cup_id = '1'");
if($result->num_rows > 0)
{
while($rs = $result->fetch_assoc())
{
$competitors[]=$rs['name'];
}
}
echo "<pre />";
print_r($competitors);
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! :/