0

I am trying to populate a dropbox values by fetching data from mysql database using PHP. Here is the code I am trying to use

    <?php
       $con = mysql_connect("localhost","root","");
      if (!$con)
       {
        die('Could not connect: ' . mysql_error());
       }
       mysql_select_db("rumi", $con);
       $resultList = mysql_query("SELECT item FROM items");  
       while ($row = mysql_fetch_array($resultList))
       {
         echo "<option value='" . $row['item'] . "'>" . $row['item'] . "</option>";
        }
       echo "</select>";
       mysql_close($con);
     ?>

but I am encountering with following error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\Lst\index.php on line 22 can you lease let me know why this is happening? can you also let me know how i can list the items in am HTML Unordered list?

user1760110
  • 2,256
  • 5
  • 29
  • 37
  • Possible duplicate [mysql_fetch_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select?answertab=votes#tab-top) – John Conde Dec 30 '12 at 05:27

4 Answers4

1

HTML Unordered list works like this:

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
</ul>

As for the error, it looks like something's wrong with your connection/query. Try echo mysql_error(); after mysql_query(...) to see what's the cause.

mav
  • 1,230
  • 1
  • 15
  • 23
  • hi mav thanks for hint . this is the error: Unknown column 'item' in 'field list'" what does this mean? I already list all values from database in the page – user1760110 Nov 05 '12 at 21:21
1

Try something like this:

$query= mysqli_query($con,"
     SELECT item FROM items") or die("Could not connect: 2".mysql_error());

This will tell you what kind of SQL error you are getting.

Alex
  • 542
  • 5
  • 24
  • Hi alex, this the error :Unknown column 'item' in 'field list'" but I already list the table in page so why now I am getting this? – user1760110 Nov 05 '12 at 21:25
  • SQL goes like this : SELECT FROM , make sure that item and items are valid names
    – Alex Nov 05 '12 at 21:30
  • Thanks Alex you are right i made a silly mistake here both table and filed name are items. I fixed the query but this time I am not getting any thing on the page. Not even error message! – user1760110 Nov 05 '12 at 21:42
0

You need to check for MySQL errors more than once - you should check after attempting to select a database and after running a query:

if (!mysql_select_db("rumi", $con)) die(mysql_error());
if ($resultList === false) die(mysql_error());

Also you should be using the mysqli_* functions as mysql is depreciated: http://us2.php.net/manual/en/intro.mysql.php

leepowers
  • 37,828
  • 23
  • 98
  • 129
0

Try adding to the top of your php page:

error_reporting(E_ALL); 
ini_set( 'display_errors','1');

Not getting anything on the page may indicate a php error. This code will display that error for you. Also, I noticed there is no open select tag. Just a /select

Alex
  • 542
  • 5
  • 24