Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql connection

Status
Not open for further replies.

akaballa123

Technical User
Apr 29, 2008
46
US
hi im a phph newb and have a problem with my code.

I have created a code to retrieve information from a field in a mysql database. I am displaying the information in a drop down field through running a while loop:

<?php

$result = @mysql_query('SELECT trainingName FROM addtrainingTbl');

while($score=mysql_fetch_array($result))
{
//Array or records stored in $score
echo "<option value='first'>$score[trainingName]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>

When I load my form page, my drop down list is blank. However, when I run an echo statement to check if the field info is being displayed, the information displays on my screen. So I know that the database connection is correct.
Is there a syntax error, or is there something wrong with my code?

Thanks
 
Should you be opening a <select> tag at some point in that code?

You have a closing </select> tag, but from in the code you posted you have no opening tag.

Adding

Code:
$result = @mysql_query('SELECT trainingName FROM addtrainingTbl');
[red]echo "<select name='myselect'>";[/red]
while($score=mysql_fetch_array($result))
  {
    //Array or records stored in $score
    echo "<option value='first'>$score[trainingName]</option>";
    /* Option values are added by looping through the array */
  }
echo "</select>";// Closing of list box

Should likely make it work.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Is there a syntax error,
You tell us. We are not magicians.
It would off course greatly help to
[ul][li]NOT suppress errors[/li]
[li]NOT rely on undefined constants automagically getting the value of their name[/li]
[li]NOT rely on a global and unspecified database connection[/li][/ul]
Off course, some error handling would not hurt either. Furthermore, I think your "first" option is repeated for every row in the result set. The fact that you get an empty dropdown seems to indicate that no rows were returned.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Wouldn't it make more sense to have individual values for your select?
Code:
 //Array or records stored in $score
    echo "<option value='[COLOR=blue]$score[trainingName][/color]'>$score[trainingName]</option>";
    /* Option values are added by looping through the array */

Greg
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top