imgrooot Posted February 27, 2017 Share Posted February 27, 2017 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); Quote Link to comment https://forums.phpfreaks.com/topic/303306-is-there-a-more-efficient-method-to-compress-these-queries/ Share on other sites More sharing options...
Barand Posted February 27, 2017 Share Posted February 27, 2017 One query, get all users and store in an indexed array. Not separate numbered variables. Quote Link to comment https://forums.phpfreaks.com/topic/303306-is-there-a-more-efficient-method-to-compress-these-queries/#findComment-1543468 Share on other sites More sharing options...
imgrooot Posted February 27, 2017 Author Share Posted February 27, 2017 One query, get all users and store in an indexed array. Not separate numbered variables. Can you please show me an example? Quote Link to comment https://forums.phpfreaks.com/topic/303306-is-there-a-more-efficient-method-to-compress-these-queries/#findComment-1543474 Share on other sites More sharing options...
Barand Posted February 27, 2017 Share Posted February 27, 2017 see https://forums.phpfreaks.com/topic/303305-pdo-looping/?p=1543471&do=findComment&comment=1543471 Quote Link to comment https://forums.phpfreaks.com/topic/303306-is-there-a-more-efficient-method-to-compress-these-queries/#findComment-1543478 Share on other sites More sharing options...
imgrooot Posted February 28, 2017 Author Share Posted February 28, 2017 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/303306-is-there-a-more-efficient-method-to-compress-these-queries/#findComment-1543479 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.