I currently have a query that checks whether a certain word is found in the array values.
$imploded = implode("','",$company_array);
$query = "SELECT *
FROM $wpdb->usermeta
WHERE meta_key='company'
AND meta_value IN ('".$imploded."')";
The $company_array contains different values.
I want to add another WHERE clause but don't know how.
It should check on the users role:
SELECT*
FROM $wpdb->usermeta
WHERE meta_key = 'wp_capabilities'
AND meta_value = 'subscriber'
Maybe some sort of join queries? I have no clue.
==== Sample data and expected output ====
This is my current query
$query = "SELECT *
FROM $wpdb->usermeta
WHERE meta_key='company'
AND meta_value IN ('".$imploded."')";
I want to add the following query
SELECT*
FROM $wpdb->usermeta
WHERE meta_key = 'wp_capabilities'
AND meta_value = 'subscriber'
The first query loads a list of users that work for a certain company.
The problem is................
Crap. I just realised something while writing this.
The problem was that the administrator is also listed in the list of users. I wanted him out of the results.
The users are listed based upon their value in the company field. But if that is empty the admin isn't listed anywhere.
Your WHERE clause needs to be
WHERE (meta_key='company' AND meta_value IN ('".$imploded."'))
OR (meta_key = 'wp_capabilities' AND meta_value = 'subscriber')
try like this
$query = "SELECT *
FROM $wpdb->usermeta
WHERE (meta_key='company'
AND meta_value IN ('".$imploded."')) or (meta_key = 'wp_capabilities' and meta_value = 'subscriber')";
I think you required below. Note the () with OR for your both where clauses.
$query = "SELECT *
FROM $wpdb->usermeta
WHERE ( meta_key='company' AND meta_value IN ('".$imploded."')")
OR (meta_key = 'wp_capabilities' AND meta_value = 'subscriber');
Related
Hi im trying to get some custom content from the database. I tryed in phpMyadmin:
SELECT DISTINCT meta_value FROM wp_usermeta WHERE meta_key = 'company'
Which returns the correct content.
Im now trying to get the content in my plugin. But its returning null
global $wpdb;
$companys = $wpdb->get_results("SELECT DISTINCT meta_value FROM wp_usermeta WHERE meta_key = 'company'");
var_dump($companys);
Im new to sql, and googled for hours, but with no result...
You just need to use DISTINCT to your SQL query, something like:
global $wpdb;
$companys = $wpdb->get_results("SELECT DISTINCT(meta_value) FROM $wpdb->usermeta WHERE meta_key = 'company'" );
var_dump($companys);
Good evening all.
Is there one that will give me an example of how to make such a script here:
I try to make a while combining 2 different tables in my database to get correct mysqli_num_rows.
Like this:
$Querypris ("SELECT * FROM" wpd2_posts` where post_status = 'wc-failed' and select * From `wpd2_postmeta` where meta_key = '_ order_total' and meta_value> 300 ')
$Num_rowspris = mysqli_num_rows ($ query price);
So I want to get mysqli_num_rows from all those who are post_status = 'wc-completed' but also meta_value> 300
Hope you understand what I mean :)
Well, your php code will not work like this. At least variable names must be consistent.
You should use JOIN clause in your sql statement. I suppose, there is a column like "post_id" in both tables.
$query_price = "SELECT * FROM wpd2_posts JOIN wpd2_postmeta USING (post_id) WHERE post_status = 'wc-completed' AND meta_key = '_order_total' AND meta_value > 300";
$result = mysqli_query($connection, $query_price);
$number_of_rows = mysqli_num_rows($result);
UPDATE:
If names of the columns, referring to ID of the post, are different in those tables, for example "ID" in "wpd2_posts" and "post_id" in "wpd2_postmeta", then you should use a following query:
SELECT * FROM wpd2_posts p JOIN wpd2_postmeta pm
ON p.ID = pm.post_id
WHERE p.post_status = 'wc-completed'
AND pm.meta_key = '_order_total' AND pm.meta_value > 300
UPDATE: removed space from '_ order_total'. changed to '_order_total'
SELECT * FROM wpd2_posts,wpd2_postmeta where wpd2_posts.post_status = wc-completed and wpd2_postmeta.meta_key = _ order_total and wpd2_postmeta.meta_value > 300 and wpd2_posts.fk=wpd2_postmeta.fk
this thing might not work for not having foreign key
SELECT * FROM wpd2_posts CROSS JOIN wpd2_postmeta where wpd2_posts.post_status = wc-completed and wpd2_postmeta.meta_key = _ order_total and wpd2_postmeta.meta_value > 300
Its better to use foreign keys.
hope this will help
If your site is WordPress then use this:
global $wpdb;
$sql="SELECT * FROM wpd2_posts pt INNER JOIN wpd2_postmeta pmt ON pmt.post_id = pt.ID WHERE pt.post_status = 'wc-completed' AND pmt.meta_key = '_order_total' AND pmt.meta_value>300";
$results = $wpdb->get_results($sql, ARRAY_A );
$num_rowspris = count($results);
// OR ---------
$results = $wpdb->query($sql);
$num_rowspris = $results->num_rows;
else :
Note: $conn is your DB connection
$results = mysqli_query($conn, $sql);
$num_rowspris = mysqli_num_rows($results);
I'm building a WordPress website.
With a MySql Query I load all companies from my database that have a specific brand:
$results = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'brand'")`
brand is an array so I search for a specific value in array:
$brand = $_GET['brand'];
$brandNeedle = $brand;
if(in_array($brandNeedle, $brand[0]))
Now, my client asked to list all users that are connected to all those companies that are selected.
So I need to create a new query. Fortunately the users all have a field added to their colum that tells me where they are working.
So I thought: what if I create a array that holds all companynames that are queried from the companylist.
With this array I can select all users who have the company name in their column
So I think the query should be something like this:
SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value ='THE ARRAY'
But this doesn't work obvious because it can't search with an array in an array.
I can't seem to figure it out. Any ideas?
-- EDIT --
Okay so I did the implode function and I do something wrong:
$brand_array = array();
for($i=0; $i <count($results); $i++){
$result = $results[$i];
if ($i % 2 == 0){
$oddeven = 'even';
}else{
$oddeven = 'odd';
}
$post_id = $result->post_id;
$company_name = get_post_meta($post_id, 'company_name', true);
$brand_array[] = $company_name;
}
print_r($brand_array);
$imploded = implode(',',$brand_array);
$query = "SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value IN $imploded";
$persons = $wpdb->get_results($query);
This gives me the results I need (the two companies in a new array)
and gives me the following error:
WordPress database error: [You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'brand1,brand2' at line 1]
SELECT * FROM wp_usermeta WHERE meta_key='company' AND meta_value IN
brand1,brand2
Have you thought about using the "IN" clause on MySQL?
https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in
You would probably have something like:
$query = "SELECT * FROM ".$wpdb->usermeta." WHERE meta_key='company' AND meta_value IN ('".implode("', ', $brand)."')";
(edit: forgot to add the parenthesis around the "in" content)
Search for mysql for IN clause
SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value IN implode(',',$brand_array)
I have created a simple html form to submit two values, license plate # and state, to a php script I have written. I am using wordpress. I want the script to look up a user in the wp_usermeta table, and select the user ID which has meta_value entries which match both license_plate and state submitted in the form. I then want the script to look up the email of that user in the wp_users table. Here is the code I have:
$license_plate = $_POST['license_plate'];
$state = $_POST['state'];
$userInfo = $wpdb->get_row ( $wpdb->prepare ("
SELECT user_id, user_email
FROM $wpdb->usermeta um
INNER JOIN $wpdb->users u ON (u.ID=um.user_id)
WHERE meta_value = %s", //OR meta_value !!
$license_plate
));
So far I can get the user_email based on the license_plate meta_value, but I want to make sure the related user_id also shares the same state value, since my database several users with the same license plate # but different states.
How can I amend this query to satisfy both of these conditions? Or do I write an additional query?
Try this query,
$wpdb->prepare ("SELECT u.id, u.user_email
FROM $wpdb->usermeta plate, $wpdb->usermeta state, $wpdb->users u
WHERE plate.user_id = state.user_id
AND plate.meta_key = 'license_plate'
AND state.meta_key = 'state'
AND u.ID = plate.user_id
AND plate.meta_value = %s
AND state.meta_value = %s", $license_plate, $state)
Here is the SQLFiddle for the same: http://sqlfiddle.com/#!2/cbc58/3
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