Stumped, But It's Easy!

I am stumped on this one… I know it’s something obvious… but supposedly this is only two lines of code. I believe I have number one coded correctly, but I’m not sure about number two. I just can’t get it to display correctly. :-X What am I doing wrong on #2? Anyone??

[code]<?php

// 1. create an SQL statement to display all categories, omitting duplicates, and sort by category
$MySQL = “SELECT DISTINCT Category
FROM Movies
ORDER BY Category”;
// **********************************************************************

// execute the SQL statement
$QueryResult = @mysql_query($MySQL, $MyDSN);

// display all of the categories as options in the select list
while ($row = mysql_fetch_array($QueryResult))

{
// 2. display a category as an option, using this format: Comedy\n
echo "Comedy\n;
// **********************************************************************

} // end while

echo “\n”;
?>

[/code]

Do you have error_reporting on? You have syntax errors here:

[php]echo "Comedy\n;[/php]

Should be

[php]echo “<option value=“Comedy”>Comedy\n”;[/php]

Then to use your row value

[php]echo “<option value=”" . $row[‘Category’] . “”>" . $row[‘Category’] . “\n”;[/php]

Hmmm. I am running all the programs using Wampserv and looking at the errors I am given there. Thank you so much I’ll give this a look!! :stuck_out_tongue:

Wamp by default displays all errors (E_ALL). It should have been returning syntax errors. You should always post the errors you get when asking for help :slight_smile:

This is such an easy program… I don’t know why this is so confusing to me. I’m still not figuring this out. I don’t understand, because I’m getting an error on line 18 which doesn’t make sense to me. =-/ Am I misinterpreting this?

[code]<?php

// 1. create an SQL statement to display all categories, omitting duplicates, and sort by category
$MySQL = “SELECT DISTINCT Category
FROM Movies
ORDER BY Category”;
// **********************************************************************

// execute the SQL statement
$QueryResult = @mysql_query($MySQL, $MyDSN);

// display all of the categories as options in the select list
while ($row[] = mysql_fetch_array($QueryResult))

{
// 2. display a category as an option, using this format: Comedy\n
echo “<option value=“Comedy”>Comedy\n”;
echo “<option value=”" . $row[‘Category’] . “”>" . $row[‘Category’] . “\n”;
// **********************************************************************

} // end while

echo “\n”;
?>

[/code]

You don’t need both option lines, I only posted the first one to demonstrate syntax.

Edit:

This is invalid… again if you post your errors it’s much easier to spot. Your error is on line 14

[php]
while ($row[] = mysql_fetch_array($QueryResult))
[/php]

This should read

[php]while ($row = mysql_fetch_array($QueryResult))[/php]

Because you are not adding mysql_fetch_array to an existing array, you are defining $row as the current mysql_fetch_array result

It says: Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\Chapter 8\categories.php on line 18

I am being told “Use $row[0] to retrieve the field value. The word “comedy” should only appear on the web page, and nowhere in your PHP code.”

See my edited post. The thing with mysql_fetch_array is that it returns by default MYSQL_BOTH. Which means you can use either $row[0] or the associative array $row[‘Category’]

You can read the manual page about this here:
http://www.php.net/mysql_fetch_array

For your new error…

Is this your entire code? Because I don’t see any mysql_connect() aka $MyDSN ??

I did notice the error on line 14 after I posted and fixed it. That was my mistake (oops). Looking for the link… So far, I’m a bit more confused… lol.

I’ve tried all kinds of things… this is what my code looks like at the moment.

[code]<?php

// 1. create an SQL statement to display all categories, omitting duplicates, and sort by category
$MySQL = “SELECT DISTINCT Category
FROM Movies
ORDER BY Category”;
// **********************************************************************

// execute the SQL statement
$QueryResult = @mysql_query($MySQL, $MyDSN);

// display all of the categories as options in the select list
while ($row = mysql_fetch_array($QueryResult))

{
// 2. display a category as an option, using this format: Comedy\n
echo “<option value=”" . $row[0][‘Category’] . “”>" . $row[0][‘Category’] . “\n”;
// **********************************************************************

} // end while

echo “\n”;
?>

[/code]

You are suppressing errors here with the @ symbol:

[php]
$QueryResult = @mysql_query($MySQL, $MyDSN);
[/php]

Remove the @ symbol. Once you remove that you will get a new errors. Most likely related to the missing mysql_connect

Oh yeah, all kinds of good errors! lol

Undefined variable: MyDSN in C:\wamp\www\csis279\Chapter 8\inc_categories.php on line 14
Call Stack

Time Memory Function Location

1 0.0013 140344 {main}( ) …\inc_categories.php:0

( ! ) Warning: mysql_query() expects parameter 2 to be resource, null given in C:\wamp\www\csis279\Chapter 8\inc_categories.php on line 14
Call Stack

Time Memory Function Location

1 0.0013 140344 {main}( ) …\inc_categories.php:0
2 0.0018 140992 mysql_query ( ) …\inc_categories.php:14

( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\csis279\Chapter 8\inc_categories.php on line 18

Well I’m definitely lost now… lol.

Right… as expected.

You need to connect and select your database. Put this code at the top of your script, but replace these variables with your credentials (I have no idea what they might be)

‘mysql_username’
‘mysql_password’
‘database_name’

[php]
$MyDSN = mysql_connect(‘localhost’, ‘mysql_username’, ‘mysql_password’);
mysql_select_db(‘database_name’, $MyDSN);
[/php]

Okay, I’m kind of back to where I started. I’m not getting any error messages now. I am just getting a drop down menu with no options.

[code]<?php

// 1. create an SQL statement to display all categories, omitting duplicates, and sort by category
$MySQL = "SELECT DISTINCT Category FROM Movies ORDER BY Category";
// **********************************************************************

// execute the SQL statement
$QueryResult = @mysql_query($MySQL, $MyDSN);

echo “<select name=“txtCategory”>\n”;
echo “<option value=”">All Categories\n";

// display all of the categories as options in the select list
while ($row = mysql_fetch_array($QueryResult))
{
// 2. display a category as an option, using this format: Comedy\n
echo “”;
// **********************************************************************

} // end while

echo “\n”;
?>
[/code]

What options do you expect to see by echoing an empty string? heh

[php]
echo “”;[/php]

Lol, I know… I was just wanting to ‘reset’ everything to where I wasn’t getting a ton of errors. I just don’t know what the line should read at this point. I’m told this is what I need to do: “Use $row[0] to retrieve the field value. The word “comedy” should only appear on the web page, and nowhere in your PHP code”

Do you have any idea how this can be done?

I already told you

[php]
echo “<option value=”" . $row[0] . “”>" . $row[0] . “\n”;
[/php]

forgot to thank you for all your help :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service