Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP get SQL field names...

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

636 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