?
Solved

PHP get SQL field names...

Posted on 2004-09-12
5
Medium Priority
?
377 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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