Hi,
i would like to display the list of tables in a database in a drop down list
then when user selects a table, the table will be dispalyed on the page.
So far i've done this: -
<?
-
$dbname = 'mysql_dbname';
-
-
// Get records from database ().
-
$sql = "show tables from $dbname";
-
$result=mysql_query($sql);
-
-
if (!$result) {
-
echo "DB Error, could not list tables\n";
-
echo 'MySQL Error: ' . mysql_error();
-
exit;
-
}
-
-
// Show records by while loop.
-
while($row=mysql_fetch_row($result)){
-
echo "Table: {$row[0]}\n";
-
-
?>
-
<option value="<? echo $row[0]?>" <?if($row[0]==$select){ echo "selected"; } ?>><? echo $row[0]; ?></option>
-
<?
-
// End while loop.
-
}
-
?>
-
</select>
-
<input type="submit" name="Submit" value="Select" />
-
</form>
-
<hr>
-
<p>
-
<?
-
If you have selected from list box.
-
if(isset($select)&&$select!=""){
-
-
// Get records from database (table "name_list").
-
$result=mysql_query("show columns from $select");
-
$row=mysql_fetch_assoc($result);
-
?>
But i am unable to extract the list of the database into the drop down list .
Because of that i am unable to proceed with displaying the table content at the page.
Please do help.
8 7535
Hi bimeldip
For future reference you should wrap code in code tag - it makes it easier to read.
When you say it is not working what exactly is not workiong for you - what is it that you expect to happen and what is it that is actually happening?
I have done something like this - written the results of SQL to a dropdown list and the code I use is below. I should sy that the SQL result has been put into an associative array before this code executes.
[php]
<?php
echo "<select name=" . "'" . "list" . $lnGetType . "'" . "id=" . "'" . "list" . $lnGetType . "'" . ">";
foreach($laResults as $lcDataLine)
{
echo "<option value=" . $lcDataLine['ID'] . " ";
if($lcDataLine['isDefault'])
{
echo "selected='selected'";
}
echo ">" . $lcDataLine['description'] ."</option>";
}
echo "</select>";
?>
[/php]
There are obvioulsy nuances in that snippet that are particular to my database and my query (not shown) but the general idea should get you up and running.
I find code easier to read and maintain if I don't switch between languases as I go. Keeping it all in the most appropriate language is much better.
hi,
i did say whay i was expecting out of the codes- at the end of the post
I did explain i want to display the list of the tables in the database.
And i dont really get your codes...I guess its too complex for my liking.I am currently trying to understand it. But something tells me the codes are to display the content of a field in a dropdown list not to display the list of tables found in a certain database.
thanks
Hi,
I know in some database applications you can select from the datasbe just the same as you can table. I don't know if that is possible in MySQL.
The one option would be to have a table, in the dtasbase, that lists all the tables in the datasbe. You could then run the select from that table and output the resuls in a drop down box using the code I posted before.
I did have a quick hunt around to see if you could run SQL on the database as a table but I couldn't find anyting.
Perhaps a new table to list the tables is required?
Cheers
nathj
I'm not an expert of PHP but few suggestions (and information rater)
TO nathj
well.. it works like “show tables from db_name” in MySql...
where db_name is your database name....
TO bimeldip
try this -
while(list($row)=mysql_fetch_row($result)){
-
echo "Table: {$row[0]}\n";
-
instead of -
while($row=mysql_fetch_row($result)){
-
echo "Table: {$row[0]}\n";
rpnew, please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Heya, bimeldip.
Try this: -
mysql_select_db($dbname);
-
$_res = mysql_query('SHOW TABLES');
-
Hey,
Thanks for the help,i managed to display the list of the tables that are in the database in the drop down list.
Now i would like to display the content of the chosen table from the drop down list. For instance, a user choses "table A" from the drop down list,
Then the content of "table A" will be displayed below.
The content may be the fields and the columns. -
<option value="<? echo $row?>" <?if($row==$select){ echo "selected"; } ?>><? echo $row; ?></option>
-
<?
-
// End while loop.
-
}
-
?>
-
</select>
-
<input type="submit" name="Submit" value="Select" />
-
</form>
-
<hr>
-
<p>
-
<?
-
// If you have selected from list box.
-
if(isset($select)&&$select!=""){
-
-
// Get records from database (table "name_list").
-
$result=mysql_query("show columns from $row");
-
$row1=mysql_fetch_assoc($result);
-
?>
Bimeldip, please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Geoff Soper |
last post by:
I'm having problem with images becoming corrupted after loading them into a
MySQL database and extracting them again. I'm pretty sure it's todo with
escaping slashes but I'm at a loss as to how to...
|
by: Xerxes |
last post by:
Hi,
I would like to import the tables from MySQL database into Access
database and I am not sure how to go about it. I tired to "Import
External Data" from Access, selected "ODBC database" from...
|
by: anonymous |
last post by:
Hi Folks,
I have a form with two Dropdown list boxes, which get loaded with data from
Database.
DropDownList1 gets data from Table1
and
DropDownList2 gets data from Table2
Table1 has a...
|
by: jung_h_park |
last post by:
From: jung_h_park@yahoo.com
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Dropdown List not retaining its SelectedValue
Date: Mon, 26 Jun 2006 21:02:57 -0700
Hello,
My...
|
by: eureka |
last post by:
Hi All,
I'm training in Servlets, JSP and JavaScript,
I have a web page in which there's a "StudentName" textbox and
below it is a "Names" Dropdown list. Initially the Textbox is empty
and...
|
by: fpcreator2000 |
last post by:
Hello everyone. I'm having problems with a page I've created that is used to insert data into a database.
It uploads two files into two distinct folder, and it takes the filenames and inserts...
|
by: abhi3211 |
last post by:
i am using java inside java script page.
in that page i want to use two dropdown list.
in first dropdown list i am getting data from ms-access database.
in second dropdown list i want to get data...
|
by: Paul |
last post by:
Hi all,
I have a page that has a form on it which has a dropdown list on it. It
connect to an sql database and populate the list. What I would like to do is
make the list editable so that if the...
|
by: ndedhia1 |
last post by:
I have data in a database that looks like this:
INSERT INTO ALARMNOTIFICATIONDETAIL VALUES ('1865', '2295', '15401', '1', 'Prodprdcons11InstrumentationMonitor', 'ALL');
INSERT INTO...
|
by: Gary Baardman |
last post by:
I am having a few problems returning records from a mySQL database using PHP.
Firstly I must say I am trying to create a video album using MySQL, PHP and using pagination.
My first problem is...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |