stevesimo Posted May 20, 2007 Share Posted May 20, 2007 Hi, does anyone know how to assign the rows of a query result to an array? Thanks Steve Quote Link to comment https://forums.phpfreaks.com/topic/52250-assign-query-results-to-array/ Share on other sites More sharing options...
MadTechie Posted May 20, 2007 Share Posted May 20, 2007 erm mysql_fetch_array if you want all records then your need to use a loop <?php mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); mysql_select_db("mydb"); $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf("ID: %s Name: %s", $row["id"], $row["name"]); } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52250-assign-query-results-to-array/#findComment-257773 Share on other sites More sharing options...
stevesimo Posted May 20, 2007 Author Share Posted May 20, 2007 Hi, I am trying to convert an ASP function to PHP which creates two linked dropdown boxes. Here is the code. The ASP function uses getrows to assign values to the array. I have used a loop but with no success. Can you see what I am doing wrong? Thanks, Steve <form action="searchCards.asp" method="post" name="searchForm" id="searchForm" style="margin-bottom: 4; margin-top: 2; margin-right: 2;margin-left: 2;"><div align="center"> <strong> <? $strSQL = "SELECT x.categoryID, x.categoryName, y.subCategoryID, y.subCategoryName FROM Categories AS x INNER JOIN SubCategories AS y ON x.categoryID = y.categoryID ORDER BY x.categoryName, y.subCategoryName;"; $query = mysql_query($strSQL); $arrRS = array(); while ($rows = mysql_fetch_array($query)){ $arrRS[] = $rows; } ?> </strong><strong>Category</strong></span> <?= buildLinkedList("listCategorys",0,1,1,"Select business category","width150","listSubCategorys",2,3,1,"Select business type","width150") ?> <? function buildLinkedList($list1name,$list1value,$list1text,$list1size,$list1prompt,$list1class,$list2name,$list2value,$list2text,$list2size,$list2prompt,$list2class) { $strList1 = "<select class='".$list1class."' name='".$list1name."' size='".$list1size."' onchange='getList2(this);'>"."\n"; $strList1 = $strList1."<option value='' selected='selected'>".$list1prompt."</option>"."\n"; for ($i=0; $i<=uBound($arrRS,2; $i++) {); if ($arrRS[$list1text,$i] != $strValue) { $strValue = $arrRS[$list1text,$i]; $strList1 = $strList1."<option value='".$arrRS[$list1value,$i]."'>"; $strList1 = $strList1.$arrRS[$list1text,$i]."</option>"."\n"; if (strlen($strAssocArr) > 0) { $strAssocArr = left($strAssocArr, strlen($strAssocArr) - 3)."\n".");"."\n"; } $strAssocArr = $strAssocArr.'assocArray["'.$list1name."=".$arrRS[$list1value,$i].'"] = new Array('."\n"; $strAssocArr = $strAssocArr.'"","'.$list2prompt.'",'."\n"; } $strAssocArrElem = '"'.$arrRS[$list2value,$i].'", "'.$arrRS[$list2text,$i].'",'."\n"; $strAssocArr = $strAssocArr.$strAssocArrElem; } $strList1 = $strList1."</select>"."\n"; //build the output for the second list $strList2 = '<strong>Sub-Category </strong>'; $strList2 = $strList2."<select class='".$list2class."' name='".$list2name."' size='".$list2size."'>"."\n"; $strList2 = $strList2."<option value=''> </option>"."\n"; $strList2 = $strList2."</select>"."\n"; //build the associative array to populate list 2 $strAssocArr = mid($strAssocArr, 1, strlen($strAssocArr)-3).");"; $strArray = "<script type='text/javascript' language='javascript'>"."\n"; $strArray = $strArray."var assocArray = new Object();"."\n"; $strArray = $strArray.$strAssocArr."\n"; //build the javascript functions to operate the listboxes $strFunctions = "function getList2(listOptions){"."\n"; $strFunctions = $strFunctions." var thisform = listOptions.form;"."\n"; $strFunctions = $strFunctions." clearDropDown(thisform.".$list2name.");"."\n"; $strFunctions = $strFunctions.' var newvalue = listOptions.name + "=" + listOptions.options[listOptions.selectedIndex].value;'."\n"; $strFunctions = $strFunctions." fillDropDown(thisform.".$list2name.", newvalue); // fill the 2nd dropdown options"."\n"; $strFunctions = $strFunctions."}"."\n"; $strFunctions = $strFunctions."function clearDropDown(listOptions){"."\n"; $strFunctions = $strFunctions." for (var i = listOptions.options.length - 1; i >= 0; i--){"."\n"; $strFunctions = $strFunctions." listOptions.options[i] = null;"."\n"; $strFunctions = $strFunctions." }"."\n"; $strFunctions = $strFunctions." listOptions.selectedIndex = -1;"."\n"; $strFunctions = $strFunctions."}"."\n"; $strFunctions = $strFunctions."function fillDropDown(listOptions, vValue){"."\n"; $strFunctions = $strFunctions.' if (vValue != "" && assocArray[vValue]){'."\n"; $strFunctions = $strFunctions." var arrX = assocArray[vValue];"."\n"; $strFunctions = $strFunctions." for (var i = 0; i < arrX.length; i = i + 2){"."\n"; $strFunctions = $strFunctions." listOptions.options[listOptions.options.length] = new Option(arrX[i + 1], arrX[i]);"."\n"; $strFunctions = $strFunctions." }"."\n"; $strFunctions = $strFunctions.' } else listOptions.options[0] = new Option("", "");'."\n"; $strFunctions = $strFunctions."}"."\n"; $strFunctions = $strFunctions."</script>"."\n"; return $strList1."\n".$strList2."\n".$strArray."\n".$strFunctions."\n"; } ?> <input type="submit" name="Submit" value="Search"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/52250-assign-query-results-to-array/#findComment-257841 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.