Jump to content

Need help with a querying a partial column...


Jim R

Recommended Posts

Here is where I'm starting:

 

SELECT * FROM wp_usermeta WHERE meta_key = 'wp_1_s2member_custom_fields' AND meta_value LIKE '%3%'

 

As the PHP produces the page, I'm going to have 15 different options coming from the

meta_value LIKE

  part.  I'm assuming I can set up my 15 different DIVs, each with their own IF  statement involving the meta_value. 

 

Right?  I'm not going to have to have 15 different queries, am I?

 

 

I assume it would start like this:

$query = 'SELECT * FROM wp_usermeta WHERE meta_key = "wp_1_s2member_custom_fields"';
$results = mysql_query($query);
while($line = mysql_fetch_assoc($results)) 

 

But from there, I don't know if it's possible to deal with partial data with IF statements.

 

 

You only need the one query, and you shouldn't use SELECT * - name the field(s) that you want to return explicitly.  what you want can be done using if and substr.  see http://php.net/manual/en/function.substr.php for info about substr use

  Quote

Yes your one query should be fine.  You can make an "if" using $line['meta_value'] inside the loop.  If you want the results in a particular order you can use an "ORDER BY" clause in your SQL query.

 

I haven't read what Muddy linked yet, but it might answer this question.  Does the *if*....$line['meta_value'] work if I'm just trying to get part of the data in that field? 

 

Here is the raw data:

 

a:1:{s:6:"county";s:1:"4";}

 

From that, I'm going to look for "county" and then figure out if the "#" at the end is 1,2,3,4, or 5.

 

 

Ok...I'm trying to get it to count the number of relevant items, display the User ID's then show the total number that match.

 

$query = 'SELECT * FROM wp_usermeta WHERE meta_key = "wp_1_s2member_custom_fields"';
$results = mysql_query($query);
while($line = mysql_fetch_assoc($results)) {


$meta_value = unserialize($line['meta_value']);
if ($meta_value['county'] == '1' || $meta_value['county'] == '2') {
	$count1=0;

	foreach ($line as $user) {
	$count1=$count1+1;
	echo $user['user_id'] . '<br> ';
	}

echo $count1;
}
}

 

That code produced:  1 4 w a 41 4 w a 4

 

:)

 

It should show:

 

439

440

2

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.