TGWSE_GY Posted June 1, 2009 Share Posted June 1, 2009 I am asking this because I am in the pseudo code phase, and I am wanting to know how to process a MYSQL QUERY held in $query into a PHP ARRAY in $ArrayX. I read up on the sort() function however I do not see how this applies to what I am attempting to do. Can anyone help? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/ Share on other sites More sharing options...
Alex Posted June 1, 2009 Share Posted June 1, 2009 You mean just getting the result of a query into an array? $result = mysql_query($sql); $row = mysql_fetch_assoc($result); //or this if it returns more than 1 row while($row = mysql_fetch_assoc($result)) { } Quote Link to comment https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/#findComment-847304 Share on other sites More sharing options...
TGWSE_GY Posted June 1, 2009 Author Share Posted June 1, 2009 in the while loop is where I build the array built off of the $row correct? Quote Link to comment https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/#findComment-847305 Share on other sites More sharing options...
Maq Posted June 1, 2009 Share Posted June 1, 2009 What are you sorting on? You can probably accomplish this right in the query. mysql_fetch_array & mysql_fetch_assoc return an array. In AlexWD's example, $row is an associative array, you can sort by the value. Quote Link to comment https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/#findComment-847307 Share on other sites More sharing options...
TGWSE_GY Posted June 1, 2009 Author Share Posted June 1, 2009 I am populating a list of check boxes in my php script based off of 2 tables in my mysql db 1 holding the users selections and one holding the keys to those numeric values. I then am wanting to populate all the check boxes and have the checkboxes that corrolate to the value held in tbl 1 displayed as checked. Quote Link to comment https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/#findComment-847319 Share on other sites More sharing options...
TGWSE_GY Posted June 2, 2009 Author Share Posted June 2, 2009 Maybe this will help make more sense here is the code I have so far....... <?php //Connect to databasee include('/home/thegayestever/thegayestcommunityever.com/projects/recode/php/includes/configs/db_config.php'); $varCustID = $_SESSION['CustID']; // Check for Null in entire custID $queryUserData = mysql_query("SELECT * FROM Profile_Interests WHERE CustID = '$varCustID'"); $queryUserDataResult = $queryUserData; $queryAnswer = mysql_query("SELECT * FROM answ_interests"); $queryAnswerResult = $queryAnswer; while ($row = mysql_fetch_array($queryAnswer) or die(mysql_error())) { $value = $row[0]; $txt = $row[1]; while ($row2 = mysql_fetch_array($queryUserDataResult)) { if (mysql_query("SELECT InterestID FROM Profile_Interests WHERE CUSTID = '$varCustID' AND InterestsID = '$value'")) { echo "<option type=\"checkbox\" checked=\"checked\" value=\"$value\" name=\"form" . $row[2] "\">$txt"; } else { echo "<option type=\"checkbox\" value=\"$value\" name=\form" . $row[2] "\">$txt"; } } } ?> I am still debugging it but if anyone knows of an easier way please let me know. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/#findComment-847436 Share on other sites More sharing options...
gizmola Posted June 2, 2009 Share Posted June 2, 2009 You should read up on how to do joins. Probably everything you want to do can be done in one query. Quote Link to comment https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/#findComment-847442 Share on other sites More sharing options...
Ken2k7 Posted June 2, 2009 Share Posted June 2, 2009 Let me get this straight, your Profile_Interests table has both an InterestID and an InterestsID column? Quote Link to comment https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/#findComment-847490 Share on other sites More sharing options...
TGWSE_GY Posted June 2, 2009 Author Share Posted June 2, 2009 Ken no both tables have an InterestsID column, but Profile_Interests has an Interest_Index, CustID, InterestID columns in that order/ I got it working here is the code for the working script : <?php session_start(); //Connect to databasee include('/home/thegayestever/thegayestcommunityever.com/projects/recode/php/includes/configs/db_config.php'); // Check for Null in entire custID //Get the Customers ID # $varCustID = $_SESSION['CustID']; //Gets the record that corrisponds // $testUserData = mysql_query("SELECT * FROM Profile_Interests WHERE CustID='$varCustID'"); $queryAnswer = mysql_query("SELECT * FROM answ_interests"); while ($row = mysql_fetch_array($queryAnswer)) { $answ_value = $row[0]; $answ_txt = $row[1]; $answ_formName = $row[2]; $count = 0; $testUserData = mysql_query("SELECT InterestsID FROM Profile_Interests WHERE InterestsID = '$answ_value'"); if (mysql_num_rows($testUserData) == 1) { echo "<input type=\"checkbox\" checked value=\"$answ_value\" name=\"$answ_formName\">$answ_txt"; } else { echo "<input type=\"checkbox\" value=\"$answ_value\" name=\"$answ_formName\">$answ_txt"; } } ?> Now my only question is if I have all my check boxes displaying on the page, and the user checks all but one then the one that is not checked will not pass a value to the script thats calling that $_POST variable correct? Quote Link to comment https://forums.phpfreaks.com/topic/160548-solved-mysql-query-into-php-array/#findComment-847773 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.