[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

foreach not working my MySQL query

Hello,

My code returns this:

<select name="FolderID">
  <option value="2">2</option>
  <option value="M">M</option>
</select>

Instead of this:
<select name="FolderID">
  <option value="21">Main</option>
  <option value="23">Sub</option>
  <option value="25">Tree</option>
</select>

<?php
	$strQry = "SELECT FolderID, FolderName FROM cms_Folders";
	$nResult = mysql_query($strQry);
	foreach (mysql_fetch_object($nResult) as $row)
             {
             echo '<option value="' . $row['FolderID'] .'">' . $row['FolderName'] . '</option>';
             }
?>

Open in new window

0
hankknight
Asked:
hankknight
  • 3
3 Solutions
 
gnarCommented:
With mysql_fetch_object, shouldn't it be something like $row->folder_id instead of $row['folder_id']?  I think the latter one (that you're using) would be correct with mysql_fetch_array.
0
 
nplibCommented:
do it like this.
<?php
   $strQry = "SELECT FolderID, FolderName FROM cms_Folders";
   $nResult = mysql_query($strQry);
   while ($row = mysql_fetch_object($nResult))
   {
      echo '<option value="' . $row['FolderID'] .'">' . $row['FolderName'] . '</option>';
   }
?>

Open in new window

0
 
Cornelia YoderArtistCommented:

 <?php
      $strQry = "SELECT FolderID, FolderName FROM cms_Folders";
      $nResult = mysql_query($strQry);
      while ($row=mysql_fetch_array($nResult))
             {
             echo '<option value="' . $row['FolderID'] .'">' . $row['FolderName'] . '</option>';
             }
?>
0
 
nplibCommented:
oops I ment
<?php
   $strQry = "SELECT FolderID, FolderName FROM cms_Folders";
   $nResult = mysql_query($strQry);
   while ($row = mysql_fetch_array($nResult))
   {
      echo '<option value="' . $row['FolderID'] .'">' . $row['FolderName'] . '</option>';
   }
?>

Open in new window

0
 
nplibCommented:
and to do it with the mysql_fetch_object() function
<?php
   $strQry = "SELECT FolderID, FolderName FROM cms_Folders";
   $nResult = mysql_query($strQry);
   while ($row = mysql_fetch_object($nResult))
   {
      echo '<option value="' . $row->'FolderID' .'">' . $row->'FolderName' . '</option>';
   }
?>

Open in new window

0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now