Solved

PHP get SQL field names...

Posted on 2004-09-12
5
363 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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 count occurrences of each item in an array.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now