Hi:
I'm using the following code to retrieve tables and fields from the database:
PHP Code:
<?php
if ($_POST['submit']) {
//=========================== TABLE NAMES ==============================================
//Connect to the database
$conn = mysql_connect("localhost", "root") or die(mysql_error());
//choose the database to be used
$db = mysql_select_db("brc_dbase", $conn) or die(mysql_error());
//Get all tables that exist in the database
$result=mysql_list_tables("brc_dbase");
//Assign the values
mysql_fetch_array($result);
//Get the tablenames and display them
echo "<select name =\"select_table\">";
for ($i=0; $i < mysql_fetch_array($result); $i++) {
echo "<pre> <option value=".mysql_tablename($result, $i).">".mysql_tablename($result, $i)." </pre>";
}
}
echo " <tr>
<td><input type=submit name=\"submit\" value=\" GO \"><td><strong> SET </strong></td>
</td>
</tr>";
//=================================================== GET FIELDS FOR EACH TABLE =============================
//Get all fields that exist in the MAINLOGIN table
$result=mysql_list_fields("brc_dbase", "mainlogin");
//Get all fields that exist in the COMPANY_INFO table
$result=mysql_list_fields("brc_dbase", "company_info");
//Get all fields that exist in the FINISHING table
$result=mysql_list_fields("brc_dbase", "finishing");
//Get all fields that exist in the ORDER_DESC table
$result=mysql_list_fields("brc_dbase", "order_desc");
//Get all fields that exist in the ORDER_INFO table
$result=mysql_list_fields("brc_dbase", "order_info");
//Get all fields that exist in the PAPER_NEEDED table
$result=mysql_list_fields("brc_dbase", "paper_needed");
//Get all fields that exist in the PRESSMAN table
$result=mysql_list_fields("brc_dbase", "pressman");
//=================================================== ASSIGN AND DISPLAY FIELDS =============================
//Assign the values
mysql_fetch_array($result);
//Get the fieldnames and display them
echo "<select name =\"select_field\">";
if ($option_value == $mainlogin) {
for ($i=0; $i < mysql_fetch_field($result); $i++) {
echo "<pre><option value=".mysql_field_name($result, $i).">".mysql_field_name($result, $i)."</pre>";
}
}
else if ($option_value == $company_info) {
for ($i=0; $i < mysql_fetch_field($result); $i++) {
echo "<pre><option value=".mysql_field_name($result, $i).">".mysql_field_name($result, $i)."</pre>";
}
}
else if ($option_value == $finishing) {
for ($i=0; $i < mysql_fetch_field($result); $i++) {
echo "<pre><option value=".mysql_field_name($result, $i).">".mysql_field_name($result, $i)."</pre>";
}
}
else if ($option_value == $order_desc) {
for ($i=0; $i < mysql_fetch_field($result); $i++) {
echo "<pre><option value=".mysql_field_name($result, $i).">".mysql_field_name($result, $i)."</pre>";
}
}
else if ($option_value == $order_info) {
for ($i=0; $i < mysql_fetch_field($result); $i++) {
echo "<pre><option value=".mysql_field_name($result, $i).">".mysql_field_name($result, $i)."</pre>";
}
}
else if ($option_value == $paper_needed) {
for ($i=0; $i < mysql_fetch_field($result); $i++) {
echo "<pre><option value=".mysql_field_name($result, $i).">".mysql_field_name($result, $i)."</pre>";
}
}
else if ($option_value == $pressman) {
for ($i=0; $i < mysql_fetch_field($result); $i++) {
echo "<pre><option value=".mysql_field_name($result, $i).">".mysql_field_name($result, $i)."</pre>";
}
}
else {
echo "ERROR!!!!";
}
It should work as follows: Get all tables from the database, place it in a drop-down menu.
Select a table from the menu, press GO, get corresponding fields.
I get the tables, but no fields. Also, how can I make sure that the fields are retrieved only when I press "GO" and not before? Can someone show me where I'm wrong? Thanks!
Last edited by common_sense; 03-24-2005 at 04:31 PM..
Reason: Code Update
|