Jump to content

Is there a more efficient method to compress these queries?


imgrooot

Recommended Posts

Say I have a table with 100 users. I want to retrieve those 100 users with each of them having unique variable name. What's the proper way to do that? I know I can create 100 different queries and retrieve them like this. But what's a better solution?

$find_sponsor_2 = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id = :user_id");
$find_sponsor_2->bindValue(':user_id', 2);
$find_sponsor_2->execute();
$result_sponsor_2 = $find_sponsor_2->fetchAll(PDO::FETCH_ASSOC);
if(count($result_sponsor_2) > 0) {
  foreach($result_sponsor_2 as $row) {
    $matrix_user_id_2         = $row['user_id'];
    $filled_positions_2         = $row['filled_positions'];
  }
} else {
  $errors[] = 'User Id 2 not found in Matrix.';
}

$find_sponsor_3 = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id = :user_id");
$find_sponsor_3->bindValue(':user_id', 3);
$find_sponsor_3->execute();
$result_sponsor_3 = $find_sponsor_3->fetchAll(PDO::FETCH_ASSOC);
if(count($result_sponsor_3) > 0) {
  foreach($result_sponsor_3 as $row) {
    $matrix_user_id_3         = $row['user_id'];
    $filled_positions_3         = $row['filled_positions'];
  }
} else {
  $errors[] = 'User Id 3 not found in Matrix.';
}

$find_sponsor_4 = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id = :user_id");
$find_sponsor_4->bindValue(':user_id', 4);
$find_sponsor_4->execute();
$result_sponsor_4 = $find_sponsor_4->fetchAll(PDO::FETCH_ASSOC);
if(count($result_sponsor_4) > 0) {
  foreach($result_sponsor_4 as $row) {
    $matrix_user_id_4         = $row['user_id'];
    $filled_positions_4         = $row['filled_positions'];
  }
} else {
  $errors[] = 'User Id 4 not found in Matrix.';

Someone mentioned I could do it like this. But my question is, how do I get unique variables for each user, such as "$matrix_user_id_2, $matrix_user_id_3, $matrix_user_id_4" as shown in my code above.

$find_sponsor = $db->prepare("SELECT user_id, filled_positions FROM matrix_2 WHERE user_id in (2,3,4)");
$find_sponsor>execute();
$result_sponsor = $find_sponsor->fetchAll(PDO::FETCH_ASSOC);
print_r($result_sponsor);
Link to comment
Share on other sites

I see your example. Here's the updated query. My question is, how do I convert the $row array into actual singular variables? Also where did "$products" come from?

$stmt = $db->prepare("SELECT user_id, filled_positions FROM matrix_2");
if ($stmt->execute()) {
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $products[$row['user_id']][] = $row;
    $products[$row['filled_positions']][] = $row;

    print_r($row);
  }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.