Trying to add items to a drop down box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shauns2007
Forum Commoner
Posts: 27
Joined: Wed Feb 02, 2011 11:34 am

Trying to add items to a drop down box

Post by shauns2007 »

Hi There

I am very new to php. Currently I'm trying to extract data from a table in my database and put it in a drop down box but I keep getting this error.

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.3\www\72793775\student_reg.php on line 9

my code as follows:

Code: Select all

<?php include("includes/header.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php // require_once("includes/functions.php"); ?>
<?php

		$courses_query = "SELECT cid,cname FROM course ORDER BY cid";
		$list_courses = mysql_query($courses_query, $connection);
		$choices = "";
		while ($result = mysql_fetch_array($list_courses)) {
			$cid = $row["cid"];
			$cname = $row["cname"];
			$choices.="<OPTION VALUE=\"$cid\">".$cname.'</option>';
		}


?>
<select name="cid">
<option value=0>Choose a Course

Code: Select all

<?php $choices; ?>
</select>

Any suggestions would be greatly appreciated.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Trying to add items to a drop down box

Post by danwguy »

where you are doing

Code: Select all

$cid = $row["cid"];
and so forth with all those variables, try using only single quote like this...

Code: Select all

$cid = $row['cid'];
shauns2007
Forum Commoner
Posts: 27
Joined: Wed Feb 02, 2011 11:34 am

Re: Trying to add items to a drop down box

Post by shauns2007 »

Thanks for the reply danwguy but that doesn't seem to be my problem as it's still not working
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Trying to add items to a drop down box

Post by danwguy »

The only other problem I see is that you are defining $result as the array items coming from the DB but calling for $row['item'].
Try using

Code: Select all

while($row = mysql_fetch_array($list_courses)) {
$cid = $row['cid'];
//and so on
shauns2007
Forum Commoner
Posts: 27
Joined: Wed Feb 02, 2011 11:34 am

Re: Trying to add items to a drop down box

Post by shauns2007 »

I had actually just tried that but it still gives me the same error as before.

My code doesn't seem to like the line as the error always points to it - while($row = mysql_fetch_array($list_courses)) {
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

Re: Trying to add items to a drop down box

Post by jim.barrett »

shauns2007 wrote:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.3\www\72793775\student_reg.php on line 9
This usually means that your query is coming back empty. Did you check your table/field names, and make sure that the records exist in the db? ie, you have a table named courses and 2 columns 'cid' and 'cname'? and that there are records in the table?
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Trying to add items to a drop down box

Post by danwguy »

That's what I was thinking too, you are calling for select from course, not courses, are you sure the table is named course and not courses?
shauns2007
Forum Commoner
Posts: 27
Joined: Wed Feb 02, 2011 11:34 am

Re: Trying to add items to a drop down box

Post by shauns2007 »

jim.barrett wrote:
shauns2007 wrote:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.3\www\72793775\student_reg.php on line 9
This usually means that your query is coming back empty. Did you check your table/field names, and make sure that the records exist in the db? ie, you have a table named courses and 2 columns 'cid' and 'cname'? and that there are records in the table?
yes, I changed the code and now the error is gone but it's not pulling anything from my db. The table has 7 fields in it. My new code as follows

Code: Select all

<?php $courses_query = "SELECT cid,cname FROM course ORDER BY cid";
                $list_courses = mysql_query($courses_query, $connection);
               echo '<select>';
               echo '<option value = "">Choose a course</option>';
               while ($row = mysql_fetch_array($list_courses)) {
                 $cid = $row['cid'];
                 $cname = $row['cname'];
                 echo '<option value="' . $row['cid'] . '">' . $row['cname'] . '</option>'; 
                }
				echo '</select>';
?>
shauns2007
Forum Commoner
Posts: 27
Joined: Wed Feb 02, 2011 11:34 am

Re: Trying to add items to a drop down box

Post by shauns2007 »

danwguy wrote:That's what I was thinking too, you are calling for select from course, not courses, are you sure the table is named course and not courses?
yeah, its definitely called course :)
shauns2007
Forum Commoner
Posts: 27
Joined: Wed Feb 02, 2011 11:34 am

Re: Trying to add items to a drop down box

Post by shauns2007 »

shauns2007 wrote:
jim.barrett wrote:
shauns2007 wrote:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.3\www\72793775\student_reg.php on line 9
This usually means that your query is coming back empty. Did you check your table/field names, and make sure that the records exist in the db? ie, you have a table named courses and 2 columns 'cid' and 'cname'? and that there are records in the table?
It seems my query is coming back empty as I tried a print_r statement but I get nothing. As follows

Code: Select all

<?php $courses_query = "SELECT cid,cname FROM course ORDER BY cid";
                $list_courses = mysql_query($courses_query, $connection);
               echo '<select>';
               echo '<option value = "">Choose a course</option>';
               while ($row = mysql_fetch_array($list_courses)) {
               print_r($row);
               //  $cid = $row['cid'];
               //  $cname = $row['cname'];
               //  echo '<option value="' . $row['cid'] . '">' . $row['cname'] . '</option>'; 
                }
				echo '</select>';
				
?>
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

Re: Trying to add items to a drop down box

Post by jim.barrett »

This may sound silly, but I've overlooked it before...
you have defined $connection earlier in the code, correct?
shauns2007
Forum Commoner
Posts: 27
Joined: Wed Feb 02, 2011 11:34 am

Re: Trying to add items to a drop down box

Post by shauns2007 »

Actually I have narrowed down my problem to my query :D Check this out

Code: Select all

<?php $courses_query = "SELECT cid,cname FROM course ORDER BY cid";
                $list_courses = mysql_query($courses_query, $connection);
                if (!$list_courses) {
                echo "yay!";
                }
?>
It echo's yay! so problem is there.
shauns2007
Forum Commoner
Posts: 27
Joined: Wed Feb 02, 2011 11:34 am

Re: Trying to add items to a drop down box

Post by shauns2007 »

jim.barrett wrote:This may sound silly, but I've overlooked it before...
you have defined $connection earlier in the code, correct?
yes, its in the connection file in my includes

Code: Select all

<?php require_once("includes/connection.php"); ?>
shauns2007
Forum Commoner
Posts: 27
Joined: Wed Feb 02, 2011 11:34 am

Re: Trying to add items to a drop down box

Post by shauns2007 »

Sorted! :D

I created a new user for my db and I didn't do it correctly. When I used my default user it worked. Now I feel like dancing :D

Thanks guys for your help! Really appreciate it
jim.barrett
Forum Newbie
Posts: 20
Joined: Tue Mar 01, 2011 5:47 am

Re: Trying to add items to a drop down box

Post by jim.barrett »

You are quite welcome! Glad you got it worked out. :D
Post Reply