Mario
Mario

Reputation: 15

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 enter image description here

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!

Upvotes: 0

Views: 83

Answers (3)

Manu
Manu

Reputation: 901

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;

Upvotes: 2

FancyPants
FancyPants

Reputation: 83

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

Upvotes: 0

DragonZero
DragonZero

Reputation: 845

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")

Upvotes: 0

Related Questions