Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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