abcorn Posted August 4, 2011 Share Posted August 4, 2011 So, I created an account here to ask this question. I have this code: <?php function userlist() { echo '<select name="user" id="user">'; $q = mysql_query("SELECT * FROM `users` ORDER BY last_name ASC, first_name ASC"); while($a = mysql_fetch_array($q)) { echo '<option value="'.$a['id'].'">'; echo $a['first_name'].' '.$a['last_name']; echo '</option>'; } echo '</select>'; } ?> The table `users` looks like this: id - int(11) email - varchar(50) first_name - varchar(50) last_name - varchar(50) phone - varchar(20) adminpassword - varchar(35) The idea is, whenever I need a dropdown of all the users in the database, I can just call that function from within the form. For example, <form method="post" action="foo.php"> <?php userlist(); ?> <input type="submit" value="Submit" /> </form> However, whenever I do this, it gives me a select with no options. A view-source reveals this: <select name="user" id="user"><br /> <b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b><<file>></b> on line <b><<line>></b><br /> </select> I am almost certain I am doing this right. I have used this kind of code many times before. I even copypasta'd the contents of mysql_query() into phpMyAdmin, and it returned the list fine. So what am I doing wrong? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/243855-confusing-mysql-issue/ Share on other sites More sharing options...
requinix Posted August 4, 2011 Share Posted August 4, 2011 Right after the mysql_query() put a echo mysql_error(); and see what error message you get. Could it be that you're missing the bit that connects to the database? Link to comment https://forums.phpfreaks.com/topic/243855-confusing-mysql-issue/#findComment-1252157 Share on other sites More sharing options...
abcorn Posted August 4, 2011 Author Share Posted August 4, 2011 Aha! Thanks. I'm building on someone else's server, and they have a calendar script that runs and apparently connects to its own database after I do mine. So I'll just reconnect. Link to comment https://forums.phpfreaks.com/topic/243855-confusing-mysql-issue/#findComment-1252162 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.