Solved

PHP get SQL field names...

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 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

12 Experts available now in Live!

Get 1:1 Help Now