I have a database which has multiple tables. I want all the table names to be displayed in a drop down html menu. I have a php function which I call on my html document.

function namealbums()
{
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$query = mysql_query("
USE photos
GO
SELECT * 
FROM sys.Tables
GO");

echo '<select name="dropdown">'; 


while ($row = mysql_fetch_array($query)) {
   echo '<option value="'.$row.'">'.$row.'</option>';
}

echo '</select>';

}
?>

This is my HTML form which calls the function.

<table width="300px" align="center" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td cellpadding="100">

<?php echo namealbums();?>

</td>
</tr>
</table>

All i get is a drop down menu with no results in it. Just empty.

Recommended Answers

All 8 Replies

Member Avatar for LastMitch

I have a database which has multiple tables. I want all the table names to be displayed in a drop down html menu. I have a php function which I call on my html document.

I'm so lost what you are trying to do?

Why are you using SQL Server scripts instead of using MYSQL?

I assume you are connected to the db and your query works?

I also mean this:

$query = mysql_query("USE photos GO SELECT * FROM sys.Tables GO");

Where is your id? How can you fetch data without an id?

Instead of this:

<?php
$query = mysql_query("USE photos GO SELECT * FROM sys.Tables GO");
echo '<select name="dropdown">';
while ($row = mysql_fetch_array($query)) {
echo '<option value="'.$row.'">'.$row.'</option>';
}
echo '</select>';
}
?>

Try this:

<?php
$query = "SELECT id FROM sys.TablesGO";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($row = mysql_fetch_array($result)){
echo "<option value=$row[id]>$row[id]</option>";
}
echo "</select>";
?>

Sorry I am using mysql not sql. But I didnt have an idea because I didnt know I needed one. I tried your new code and it returned the same result. I have no idea why.

Member Avatar for LastMitch

@garyjohnson

Sorry I am using mysql not sql.

Read this your example looks exactly like these example:

http://odachoo.blogspot.com/2012/04/querying-sql-server-system-catalog-faq.html

But I didnt have an idea because I didnt know I needed one.

You do need a DB and a query.

I tried your new code and it returned the same result. I have no idea why.

The reason why it didn't work because the query is wrong and the db is not connected.

I think I help you in the past. I am bit surprise that you don't know how to create a table or a db and create a query to fetch the data.

The code I provided does that. The only thing you need to do is connect the db and used the query to fetch the data.

What is your table? Can you post your table.

Im sorry that Im making this confusing lol but I do have a database and a table. For some reason I didnt post all of the function which connects to the databse. This is the whole function.

function namealbums()
    {
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    $query = "SELECT id FROM sys.Tables GO";
    $result = mysql_query ($query);
    echo "<select name=dropdown value=''>Dropdown</option>";
    while($row = mysql_fetch_array($result)){
    echo "<option value=$row[id]>$row[id]</option>";
}
echo "</select>";
    }
    ?>

I am not sure how im suppose to post my table but it goes like this. The database is called Photos. There are mutliple tables within the database. The columns go as is,
pic_id int (11)
location char(30)
char2 char(30)
char1 char(30)
name char(30)
album char(30)

Member Avatar for LastMitch

@garyjohnson

I am not sure how im suppose to post my table but it goes like this. The database is called Photos. There are mutliple tables within the database. The columns go as is,

This is fine.

What data are you trying to fetch? name or album?

This is for name:

<?php
$conn = mysql_connect("$db_host", "$db_username", "$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");

$query = "SELECT pic_id, name FROM Yourtable ORDER BY name";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result)){
echo "<option value=$r[pic_id]>$r[name]</option>";
}
echo "</select>";
?>

This is for album:

<?php
$conn = mysql_connect("$db_host", "$db_username", "$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");

$query = "SELECT pic_id, album FROM Yourtable ORDER BY album";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result)){
echo "<option value=$r[pic_id]>$r[album]</option>";
}
echo "</select>";
?>

This is pretty much the outline.

It worked! Thank you so much. I dont know why I had so much trouble with this. I have done this before. Thanks Though!

3.php

<html>
<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
  <script>
  $(document).ready(function() {
    $("#datepicker").datepicker({ dateFormat: "dd/mm/yy" });
      $("#datepicker2").datepicker({ dateFormat: "dd/mm/yy" });
        });
           </script>

           <?php
           function peralatan()
{
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("borrow") or die(mysql_error());
$query = "SELECT item FROM inventori ORDER BY item";
$result = mysql_query ($query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($r = mysql_fetch_array($result)){
echo "<option value=$r[id]>$r[item]</option>";
}
echo "</select>";
}
?>

   <tr>
     <td width="871" height="350">
     <p align="center" class="style12">&nbsp;</p>
     <p align="center" class="style12"></p>
     <table width="428" height="226" border="0" align="center">
<form method="post" action="4.php">
<table width="520" border="0" align="center">

  <tr>
    <td width="137"><span class="style21">Name</span></td>
        <td width="144"><input type="textbox1" name="Name[]"></td>
            <td width="82">&nbsp;</td>
                <td width="144">&nbsp;</td>
      </tr> 
  <tr>
    <td><span class="style21">Startdate</span></td>
        <td><input type="textbox6" name="Startdate[]" id="datepicker"></td>
            <td align="center"><span class="style21">sehingga</span></td>
                <td><input type="textbox7" name="Enddate[]" id="datepicker2"></td>
      </tr>
  </table>
<br />
<table width="518" border="1" align="center">
  <tr align="center">
    <th width="28" scope="col"><div align="center" class="style21 style24">Bil.</div></th>
    <th width="153" scope="col"><span class="style25">Item</span></th>
    <th width="156" scope="col"><span class="style25">Model</span></th>
    <th width="153" scope="col"><span class="style25">No. Inventori</span></th>
  </tr>

    <tr align="center">
        <td>1.</td>
            <td><select name="dropdown" name="item1[]"> <?php echo item();?></td>
                <td><input type="textbox8" name="item1[]"></td>
                    <td><input type="textbox8" name="item1[]"></td>
      </tr>
    <tr align="center">
        <td>2.</td>
            <td><input type="textbox9" name="item2[]"></td>
                <td><input type="textbox9" name="item2[]"></td>
                    <td><input type="textbox9" name="item2[]"></td>
      </tr>
    <tr align="center">
        <td>3.</td>
            <td><input type="textbox10" name="item3[]"></td>
                <td><input type="textbox10" name="item3[]"></td>
                    <td><input type="textbox10" name="item3[]"></td>
      </tr>
    <tr align="center">
        <td>4.</td>
            <td><input type="textbox11" name="item4[]"></td>
                <td><input type="textbox11" name="item4[]"></td>
                    <td><input type="textbox11" name="item4[]"></td>
      </tr>
    <tr align="center">
        <td>5.</td>
            <td><input type="textbox12" name="item5[]"></td>
                <td><input type="textbox12" name="item5[]"></td>
                    <td><input type="textbox12" name="item5[]"></td>
      </tr>


    </table>
<br />
<center><input type="submit" name="submit" value="submit"><input type="reset" name="Reset" value="reset"></center>

</form>

</body>
</html>

4.php

<?php

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("pinjaman") or die(mysql_error());

$TextBox1 = implode(', ', $_POST['Name']);
$TextBox6 = implode(', ', $_POST['Startdate']);
$TextBox7 = implode(', ', $_POST['Enddate']);
$dropdown = implode(', ', $_POST['item1']);
$TextBox8 = implode(', ', $_POST['item1']);
$TextBox8 = implode(', ', $_POST['item1']);
$TextBox9 = implode(', ', $_POST['item2']);
$TextBox9 = implode(', ', $_POST['item2']);
$TextBox9 = implode(', ', $_POST['item2']);
$TextBox10 = implode(', ', $_POST['item3']);
$TextBox10 = implode(', ', $_POST['item3']);
$TextBox10 = implode(', ', $_POST['item3']);
$TextBox11 = implode(', ', $_POST['item4']);
$TextBox11 = implode(', ', $_POST['item4']);
$TextBox11 = implode(', ', $_POST['item4']);

if(isset($_POST['submit']))
{       
    $query="INSERT INTO epal VALUES ('" . $TextBox1 . "','" . $TextBox6 . "','" . $TextBox7 . "','" . $dropdown . "','" . $TextBox8 . "','" . $TextBox9 . "','" . $TextBox10 . "','" . $TextBox11 . "')";     

    mysql_query($query) or die (mysql_error() );

    echo "Complete";

}

?>

i try your drop down list.it work! but i have a problem to insert the data to database.really need a help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.