Jump to content

[SOLVED] Dynamic dropdown returning bool(false)


dolcezza

Recommended Posts

Can someone please educate me?

If I var_dump($result) I get bool(false)

What am I doing wrong? I am trying to create a dynamic dropdown from the database.

Any help for a beginner is appreciated.

 

$query = "SELECT * FROM authors";
   $result = mysql_query($query);
     if(mysql_num_rows($result)) {
       // we have at least one user, so show all users as options in select form
       while($row = mysql_fetch_row($result))
       {
          print("<option value=\"$row[0]\">$row[2]</option>");
       }
     } else {
       print("<option value=\"\">No users created yet</option>");
     }

Link to comment
Share on other sites

This is your complete code.

Try like this:

 

$query = "SELECT * FROM authors";
   $result = mysql_query($query);
     if(mysql_num_rows($result)) {
       // we have at least one user, so show all users as options in select form
       while($row = mysql_fetch_row($result))
       {
         echo "<select name='user'>";
          echo "<option value='$row[0]'>$row[2]</option>";
       }
     } else {
       echo "<option value=''>No users created yet</option>";
       echo "</select>";
     }

Link to comment
Share on other sites

This is your complete code.

Try like this:

 

$query = "SELECT * FROM authors";
   $result = mysql_query($query);
     if(mysql_num_rows($result)) {
       // we have at least one user, so show all users as options in select form
       while($row = mysql_fetch_row($result))
       {
         echo "<select name='user'>";
          echo "<option value='$row[0]'>$row[2]</option>";
       }
     } else {
       echo "<option value=''>No users created yet</option>";
       echo "</select>";
     }

 

I may be wrong but

  while($row = mysql_fetch_row($result))

      {

        echo "<select name='user'>";

          echo "<option value='$row[0]'>$row[2]</option>";

      }

 

in while loop we are creating <select>??

Link to comment
Share on other sites

i tried this and it worked form me kindly replicate at your end.

 

echo '<select>';

  if(mysql_num_rows($result)) {

      // we have at least one user, so show all users as options in select form

      while($row = mysql_fetch_row($result))

      {

 

          echo "<option value=\"$row[0]\">$row[0]</option>"; --change this bold stuff according to your $row output

      }

    } else {

 

      echo "<option value=\"\">No users created yet</option>";

    }

echo '</select>';

 

Regards

Link to comment
Share on other sites

still getting no users...

I have the select above it a bit further, because I wanted multi value etc..

Is this ok?

There is something wrong I think with the query, because it isn't finding the users, but I don't know what.

<form action="insertevent.php" method="POST">
    Author: <select name="authorid[]" id="authorid" multiple="true">"
<? $query = "SELECT * FROM authors";
   $result = mysql_query($query);
     if(mysql_num_rows($result)) {
       // we have at least one user, so show all users as options in select form
       while($row = mysql_fetch_row($result))
       {

          echo "<option value=\"$row[0]\">$row[2]</option>";
       }
     } else {

       echo "<option value=\"\">No users created yet</option>";
     }
    echo '</select>';
    ?>

Link to comment
Share on other sites

Yeh Rajiv is right put mysql error reporting.

 

try this code:

$query = "SELECT * FROM authors";
  $result = mysql_query($query) or die (mysql_error());
    if(mysql_num_rows($result)) {
      // we have at least one user, so show all users as options in select form
     echo "<select name='user'>";
      while($row = mysql_fetch_object($result))
      {
        
         echo "<option value=\"$row->0\">$row->$row->2</option>";
      }
}else{
      echo "<option value=''>No users created yet</option>";
      echo "</select>";
    }

Link to comment
Share on other sites

got it working....

thank you very much

 

working code:

<form action="insertevent.php" method="POST">
    Author: <select name="authorid[]" id="authorid" multiple="true">"
<? $query = "SELECT * FROM authors ORDER BY authorlast ASC";
   $result = mysql_query($query) or die("there was an error:".mysql_error());
     if(mysql_num_rows($result)) {
       // we have at least one user, so show all users as options in select form
       while($row = mysql_fetch_row($result))
       {
          echo "<option value=\"$row[0]\">$row[2]</option>";
       }
     } else {

       echo "<option value=\"\">No users created yet</option>";
     }
    echo '</select>';
    ?>

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.