Jump to content

Confusing mySQL issue?


abcorn

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.