Page 1 of 1

Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 11:34 am
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.

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 12:48 pm
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'];

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 12:55 pm
by shauns2007
Thanks for the reply danwguy but that doesn't seem to be my problem as it's still not working

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 1:49 pm
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

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 2:00 pm
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)) {

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 5:33 pm
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?

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 5:34 pm
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?

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 10:48 pm
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>';
?>

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 10:50 pm
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 :)

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 11:21 pm
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>';
				
?>

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 11:23 pm
by jim.barrett
This may sound silly, but I've overlooked it before...
you have defined $connection earlier in the code, correct?

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 11:26 pm
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.

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 11:28 pm
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"); ?>

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 11:42 pm
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

Re: Trying to add items to a drop down box

Posted: Thu Mar 17, 2011 11:53 pm
by jim.barrett
You are quite welcome! Glad you got it worked out. :D