Jump to content

PHP/MySQL Drop Down Error


CEinNYC

Recommended Posts

Hey all -

 

I (think) that I'm following the code exactly, but no luck...  Essentially I want to populate a drop down menu from text stored in mysql (varchar). 

 

Here is my code (and where I'm pulling the code found below).  Any ideas what's going wrong.  (My error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in....).

 


$sql = "SELECT * FROM table";
$results = mysql_query($sql); //or die("Query failed with error: ".mysql_error());       // execute the query
$options="";

while ($row=mysql_fetch_array($results)){
$id=$row["id"];
$thing=$row["thing"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}

 

<select name="id" style="font-size:20px;font-family:Arial;width:275px">

<option value=0>Choose<? echo $options?></option>

</select>

 

Here is where I'm getting my code sample: http://forums.devarticles.com/mysql-development-50/drop-down-menu-populated-from-a-mysql-database-1811.html

 

Thank for any advice!!

Link to comment
Share on other sites

Yeah, Noob here.  I changed the name wondering if there is a problem with, well, anything. (Throwing it all at the wall and seeing what sticks).  here is the remaining part of the error when I uncomment out "or die",

 

Query failed with error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table' at line 1

 

line 1 can be one of the following:

 

<?php

//require 'header.php';

//require 'footer.php';

mysql_connect('localhost', 'root', '');  //username and password of database

mysql_select_db('giantbattlemonsters') or die( "Unable to select database");

 

 

//$sql = "SELECT id, display_name * FROM table";

$sql = "SELECT * FROM table";

Link to comment
Share on other sites

I think that I'm narrowing down the problem.  I fixed the connection to the database and no longer get any errors with the connection, or line one.  however the drop down box is still not populating from the mysql database.  I think that I narrowed it down to this bit:

 

while ($row=mysql_fetch_array($results)){

$id=$row["something"];

$thing=$row["id"];

$options.="<OPTION VALUE=\"$id\">".$thing;

}

 

When "something" is a row name in the database, I get no errors (but no drop down populating correctly).  (using "something" for the sake of indicating the error)

So, I need know what's happening there.. "ID" - the ID in the database equals the row (in the database), "something" is....  the row name? (but that doesn't populate the drop down?

 

Thanks for the help!  Essentially for a class I'm trying to build a poor mans: http://thesurrealist.co.uk/monster

 

Link to comment
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.