473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

extracting data frm mysql database and displaying in dropdown list

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:
Expand|Select|Wrap|Line Numbers
  1.  <?
  2.  $dbname = 'mysql_dbname';
  3.  
  4. // Get records from database ().
  5.  $sql = "show tables from $dbname";
  6.  $result=mysql_query($sql);
  7.  
  8. if (!$result) {
  9.     echo "DB Error, could not list tables\n";
  10.     echo 'MySQL Error: ' . mysql_error();
  11.     exit;
  12. }
  13.  
  14.  // Show records by while loop.
  15.  while($row=mysql_fetch_row($result)){
  16.  echo "Table: {$row[0]}\n";
  17.  
  18.  ?>
  19. <option value="<? echo $row[0]?>" <?if($row[0]==$select){ echo "selected"; } ?>><? echo $row[0]; ?></option>
  20. <?
  21. // End while loop.
  22. }
  23. ?>
  24.   </select> 
  25.   <input type="submit" name="Submit" value="Select" />
  26. </form>
  27. <hr>
  28. <p>
  29.   <?
  30.  If you have selected from list box.
  31. if(isset($select)&&$select!=""){
  32.  
  33. // Get records from database (table "name_list").
  34. $result=mysql_query("show columns from $select");
  35. $row=mysql_fetch_assoc($result);
  36. ?>
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.
Aug 22 '07 #1
8 7535
nathj
938 Expert 512MB
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.
Aug 22 '07 #2
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
Aug 22 '07 #3
nathj
938 Expert 512MB
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
Aug 22 '07 #4
rpnew
188 100+
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
Expand|Select|Wrap|Line Numbers
  1. while(list($row)=mysql_fetch_row($result)){
  2. echo "Table: {$row[0]}\n";
  3.  
instead of
Expand|Select|Wrap|Line Numbers
  1. while($row=mysql_fetch_row($result)){
  2. echo "Table: {$row[0]}\n";
Aug 22 '07 #5
pbmods
5,821 Expert 4TB
rpnew, please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Aug 22 '07 #6
pbmods
5,821 Expert 4TB
Heya, bimeldip.

Try this:
Expand|Select|Wrap|Line Numbers
  1. mysql_select_db($dbname);
  2. $_res = mysql_query('SHOW TABLES');
  3.  
Aug 22 '07 #7
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.
Expand|Select|Wrap|Line Numbers
  1. <option value="<? echo $row?>" <?if($row==$select){ echo "selected"; } ?>><? echo $row; ?></option>
  2. <?
  3. // End while loop.
  4. }
  5. ?>
  6.   </select> 
  7.   <input type="submit" name="Submit" value="Select" />
  8. </form>
  9. <hr>
  10. <p>
  11.   <?
  12. // If you have selected from list box.
  13. if(isset($select)&&$select!=""){
  14.  
  15. // Get records from database (table "name_list").
  16. $result=mysql_query("show columns from $row");
  17. $row1=mysql_fetch_assoc($result);
  18. ?>
Aug 23 '07 #8
pbmods
5,821 Expert 4TB
Bimeldip, please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Aug 23 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
3
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...
4
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...
5
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...
11
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...
6
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...
5
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...
4
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...
47
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...
4
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...
0
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,...
0
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$) { } ...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.