Jump to content

mysql data & dropdown


funbinod

Recommended Posts

i created loop for selecting from all the rows of a table

like this---

<?$rows = mysql_num_rows($result);for ($n = 0 ; $n < $rows ; ++$n)    {        echo '' . mysql_result($result,$n,'item') . '<br />';    }?>

and tried to put in dropdown menu like this ----

 

echo '<form action="#" id="test" name="test">';echo '<select name="item">';echo "<option></option>";echo "<option>";{echo '' . mysql_result($result,$n,'item') . '<br />';}echo "</option>";echo '</select>';echo '</form>';

but returned error

"Warning: mysql_result(): Unable to jump to row 2 on MySQL result index 5 in .................................."

please guide me....
Link to comment
Share on other sites

You probably should use mysql_fetch_assoc() instead.

 

Best if you used a more recent database library, such as mysqli or PDO, because mysql is insecure and is being deprecated.

 

i think thats not the solution to my problem.

 

my problem IS NOT that i could not extract data from table.

 

my problem IS i coludn't put that data in a DROPDOWN list.

 

or i didn't understand ur sugestion?????

Link to comment
Share on other sites

You're getting a PHP error because you're asking for an index in mysql_result() that doesn't seem to exist. The easiest way to solve the problem is to use mysql_fetch_assoc() rather than mysql_result() to get data from the query.

 

Looking at your second code more carefully, you don't seem to have a while loop and you have a pair of braces { } that aren't representing anything.

echo '<form action="#" id="test" name="test">';echo '<select name="item">';echo "<option></option>";echo "<option>";{echo '' . mysql_result($result,$n,'item') . '<br />';}echo "</option>";echo '</select>';echo '</form>';

Somebody already provided working code for your question in this thread: http://w3schools.invisionzone.com/index.php?showtopic=49767

Link to comment
Share on other sites

You're getting a PHP error because you're asking for an index in mysql_result() that doesn't seem to exist. The easiest way to solve the problem is to use mysql_fetch_assoc() rather than mysql_result() to get data from the query.

 

Looking at your second code more carefully, you don't seem to have a while loop and you have a pair of braces { } that aren't representing anything.

echo '<form action="#" id="test" name="test">';echo '<select name="item">';echo "<option></option>";echo "<option>";{echo '' . mysql_result($result,$n,'item') . '<br />';}echo "</option>";echo '</select>';echo '</form>';

Somebody already provided working code for your question in this thread: http://w3schools.invisionzone.com/index.php?showtopic=49767

 

thank u!

 

now i understood where i did mistake.

 

actually i had used for loop to get data. but i placed that line elsewhere on the page and expected the result here. finally i completed the code by doing this ---

<?$rows = mysql_num_rows($result);for ($n = 0 ; $n < $rows ; ++$n)	{		echo '' . mysql_result($result,$n,'item') . '<br />';	}echo '<form action="#" id="test" name="test">';echo '<select name="item">';$rows = mysql_num_rows($result);for ($n = 0 ; $n < $rows ; ++$n){		echo "<option>" . mysql_result($result,$n,'item') . "</option>" ;}echo '</select>';echo '</form>';	?>

thank u for guiding me. :good:

 

(sorry! i couldn't understand the solution that was given on that thread. that's why i was continuing here also.)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...