PDA

View Full Version : Trouble building a select box from a db


whizard
11-28-2005, 08:13 PM
Hi. I have some code which is supposed to list all the usernames in a database table.


include("login/db/dbconnection.inc");
$connect = mysql_connect($dbserver,$dbuser,$dbpass) or die("Could not connect to MySQL");
$db = mysql_select_db($dbselect,$connect) or die("Could not connect to database \"<strong>$dbselect</strong>\"");
$query = "SELECT * FROM `users`";
$result = mysql_query($query, $mysql_link);
if(mysql_num_rows($result))
{
while($row = mysql_fetch_row($result))
{
print("<option value=\"$row[2]\">".$row[2]."</option>"); }
}
else
{
print("<option value=\"\">No users created yet</option>");
}


The problem is that I keep getting this result in the select box: No users created yet

I have several entries in the db however.

If anyone sees what I am doing wrong, could they tell me?

Thanks,
Dan

Velox Letum
11-28-2005, 08:21 PM
$result = mysql_query($query, $mysql_link);

You're running a query on a non-existant link. You can either get rid of the value or change $mysql_link to $connect, as that is your link id.

1) $result = mysql_query($query);
2) $result = mysql_query($query, $connect);

Unless you have multiple connections, you don't really need a link id in the query function, but you can if you prefer it.

whizard
11-28-2005, 08:39 PM
Thanks! That solved it. I copied this from another script I had that almost did the same thing, and just missed that.

Thanks,
Dan