intenziti Posted August 23, 2007 Share Posted August 23, 2007 Hi there, I came upon this thread while looking for code for a project I am completing for university. I have made a form with a drop down menu so that the user can query the database about which course is available in each month. However I keep getting this error message Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /Applications/xampp/xamppfiles/htdocs/paul/month_drop_down.php on line 41 The php code I actually got from this page: http://www.webmasterworld.com/forum88/4831.htm I have tried to adapt it to suit my purposes and it seems that all my database information is correct but for the likes of me I cannot work out why the php code is not processing the form data from the drop down menu. Would anyone here please be able to have a quick look at my code and see if there is anything that may be stopping it from functioning? There are two fields in my table which are called 'month' and 'course', the table itself is called months. Any help would be greatly appreciated! Form code: <form name="month_search" method="post" action="month_drop_down.php"><table width="600" align="center" border="0" cellspacing="1" cellpadding="2"> <tr><td width=300><select name="month" id="selCourse" style="width: 200px;"><option value="Select month to attend..." selected>Select month to attend...</option><option value="jan">January</option><option value="feb">February</option><option value="mar">March</option><option value="apr">April</option><option value="may">May</option><option value="jun">June</option><option value="jul">July</option><option value="aug">August</option><option value="sep">September</option><option value="oct">October</option><option value="nov">November</option><option value="dec">December</option></select></td><td><input type="submit" name="search" value="Search"/></td> </tr> </table></form> PHP code: <?php // get variable after selecting something from the dropdown with name 'month' $select = $_POST['search']; // if something has been chosen if (!empty($select)) { // get the chosen value $month = $_POST['month']; // select the type from the database // database connection details (change to whatever you need) $HOST = 'localhost'; $DATABASE = 'sworphe_paul'; $USER = 'sworphe_paul'; $PASSWORD = 'sworphe_paul'; // connect to database if(!$conn=mysql_connect('localhost','sworphe_paul' ,'sworphe_paul')) { echo("<li>Can't connect to $HOST as $USER"); echo("<li>mysql Error: ".mysql_error()); die; } // select database if (!mysql_select_db($DATABASE,$conn)) { echo("<li>We were unable to select database $DATABASE"); die; } // if everything successful create query // this selects all rows where the type is the one you chose in the dropdown // * means that it will select all columns, ie name and type as i said above $sql_query = "SELECT * FROM months WHERE type='$month'"; // get the data from the database $result = mysql_query($sql_query,$conn); // output data while (($deails = mysql_fetch_assoc($result))) { // print out the name echo('Courses available in '.$details['month'].' - '); // print out the type echo('Type: '.$details['course'].'<br>'); } // close mysql connection mysql_close($conn); } ?> Thank you so much in advance! ??? Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/ Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 could yo show me line 41? Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331727 Share on other sites More sharing options...
intenziti Posted August 23, 2007 Author Share Posted August 23, 2007 Sure line 41 sits just under // output data as follows: while (($deails = mysql_fetch_assoc($result))) { Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331738 Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 try. while($details = mysql_fetch_array($result)) { Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331742 Share on other sites More sharing options...
intenziti Posted August 23, 2007 Author Share Posted August 23, 2007 Unfortunately it gives the exact same error Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331749 Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 what is the error line... like PARSE ERROR or something Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331753 Share on other sites More sharing options...
Fjerpje Posted August 23, 2007 Share Posted August 23, 2007 Hi there, It means you didnt correctly close a string. Look again at your code and look if all "" and ; are correctly placed Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331755 Share on other sites More sharing options...
intenziti Posted August 23, 2007 Author Share Posted August 23, 2007 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/xampp/xamppfiles/htdocs/paul/month_drop_down.php on line 41 Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331756 Share on other sites More sharing options...
Fjerpje Posted August 23, 2007 Share Posted August 23, 2007 hi there, And that means that your SQL syntax isnt correct post it here and i and other might help you Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331759 Share on other sites More sharing options...
intenziti Posted August 23, 2007 Author Share Posted August 23, 2007 SQL syntax? Sorry I'm new to this so where exactly would I find that? Is it something I would define myself? Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331760 Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 the bit should look like ... $result = mysql_query("SELECT * FROM Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331761 Share on other sites More sharing options...
Fjerpje Posted August 23, 2007 Share Posted August 23, 2007 Or just the line 41 Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331772 Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 ok try replace // get the data from the database $result = mysql_query($sql_query,$conn); with this // get the data from the database $result = mysql_query($sql_query); Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331774 Share on other sites More sharing options...
intenziti Posted August 23, 2007 Author Share Posted August 23, 2007 No luck unfortunately. It keeps coming up with the exact same error Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331781 Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 what error? Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331786 Share on other sites More sharing options...
intenziti Posted August 23, 2007 Author Share Posted August 23, 2007 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/xampp/xamppfiles/htdocs/paul/month_drop_down.php on line 41 Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331791 Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 ok thats this bit // if everything successful create query // this selects all rows where the type is the one you chose in the dropdown // * means that it will select all columns, ie name and type as i said above $sql_query = "SELECT * FROM months WHERE type='$month'"; // get the data from the database $result = mysql_query($sql_query,$conn); // output data while (($deails = mysql_fetch_assoc($result))) { I would suggest to uyse the below then tell me what the error says: // if everything successful create query // this selects all rows where the type is the one you chose in the dropdown // * means that it will select all columns, ie name and type as i said above $result = mysql_query("SELECT * FROM months WHERE type='$month'") or die("My Error Line: ".mysql_error()); // output data while (($deails = mysql_fetch_assoc($result))) { Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331796 Share on other sites More sharing options...
intenziti Posted August 23, 2007 Author Share Posted August 23, 2007 OK now getting the following error message: My Error Line: Unknown column 'type' in 'where clause' Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331812 Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 means type doesnt exist Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331824 Share on other sites More sharing options...
intenziti Posted August 23, 2007 Author Share Posted August 23, 2007 so 'type' is referring to the table data? Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331845 Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 type does not exist in the months table Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331869 Share on other sites More sharing options...
intenziti Posted August 23, 2007 Author Share Posted August 23, 2007 Great! Thank you so much for your help! Quote Link to comment https://forums.phpfreaks.com/topic/66315-solved-php-code-for-querying-mysql-database/#findComment-331887 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.