Solved

PHP get SQL field names...

Posted on 2004-09-12
5
374 Views
Last Modified: 2013-12-12
Hi,
I have a PHP query that gets the field names of a SQL view and displays them in drop down boxes for selection.
Config....MSSQL2000, PHP5, Apache2, Redhat9.
Heres the code....
$poll_id = $_POST['poll_id'];
$query = "SELECT TOP 1 * FROM [ST_{$poll_id}_View];";
$result = mssql_query($query);
$pollarray = mssql_fetch_array($result);
print ("<form method=\"POST\" name = \"Polls_Form\" action=\"createusergraphs.php?tid=$terminal_id&action=2\">");
for ($i = 0; $i < 5; ++$i){
print ("<tr>
<td align=\"center\"><font size=\"2\">Field $i</font></td>
<td align=\"center\"><select class=\"textbox-12pt\" size=\"1\" name=\"field{$i}\">");
print ("<option selected value=\"NONE\">NONE</option>");
$result = mssql_query( $query );
$numfields = mssql_num_fields($result);
for ($x=0; $x < $numfields; ++$x){
$fieldname = mssql_field_name($result,$x);
IF ((strtolower($fieldname) == "receivedtimestamp") or (strtolower($fieldname) == "terminalid") or (strtolower($fieldname) == "customerusername") or (strtolower($fieldname) == "pollname")) $fieldname = "NONE";
ELSE print ("<option class=\"textbox-12pt\" value=\"$fieldname\">$fieldname</option>");
};
print ("</select></td></tr>");

The main problem is that I have noticed in SQL profiler that each time a View is read to create the drop down, it does over 87,000 reads.  Im guessing that it is running the view which reads the whole table, this will eventually slow to a fast crawl as the table size increases.
Is there a faster way to go about getting the field names from the table?
Thanks!
0
Comment
Question by:maunded
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 12041646
If you only need the fieldnames try this query
//query will not return any result as is allways false
$query = "SELECT * FROM [ST_{$poll_id}_View] WHERE 0 = 1;";

and remove the line $pollarray = mssql_fetch_array($result); as that is not used.  mssql_field_name should still return the names
0
 
LVL 1

Author Comment

by:maunded
ID: 12042060
Thanks.  What does the WHERE 0 = 1 do?
0
 
LVL 48

Expert Comment

by:hernst42
ID: 12042224
Its a condition which can never be true, so the SQL-optimizer knows that there can't be any rows to be returned and does not do a select on that view/table.
0
 
LVL 1

Author Comment

by:maunded
ID: 12042272
But it still returns the rows names?  In effect, thats all it returns?
0
 
LVL 1

Author Comment

by:maunded
ID: 12042316
I have just tested....thats fantastic thanks hernst42!!  It cut my page loading time to less than 1/1000th of what it was!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 75
Get data from two MySQL tables 6 63
&& operators not performing required logic 8 39
Echo'd value as selected in dropdown 20 41
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question