?
Solved

HOw do i determine if a field exists or not in my php/mysql application

Posted on 2009-04-30
5
Medium Priority
?
228 Views
Last Modified: 2013-12-13
Ok client wanted to removed three fields from thieir application.  I simply commented out the three fields in my code.  Client then realized, for old requests, they still wanted to see the data.  So I want to add code like attached, but i want to say if you are in display mode and the field Q_9 existist in the db for this record, show the attached info.

to test my theory i added q_9 back and removed data from it and it works, but the key here is Q_9 will no longer be used in the db, so how do i say if field/field name exists, ,show the data.
<?
    if ($mode=="display" && $Q['Q_9'] ==""):
    //if ($mode=="display" && !empty($Q['Q_9'])):
?>
<tr>
<td width="210" class="required"><label><?=$Q['Q_9']?></label></td>
<td width="460"><?=printRadio($mode,'Q_9',getSelectValues('Q_9'),$A['Q_9'])?></td>
</tr>
<?endif;?>
<tr>

Open in new window

0
Comment
Question by:justmelat
  • 3
4 Comments
 
LVL 7

Expert Comment

by:aamodt
ID: 24274050
this code provided will show the data when the field Q_9 exist
<?php 
 
$result = mysql_query("SELECT * from table where field = 'Q_9');
 
if(mysql_num_fields($result) == 1) {
show data
}
 
 
?>

Open in new window

0
 
LVL 17

Accepted Solution

by:
Aleksandar Bradarić earned 1600 total points
ID: 24285131
Try something like this (TABLE_SCHEMA is actually your DB name):
mysql> SELECT COLUMN_NAME FROM COLUMNS WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME = 'myTable';
+-------------+
| COLUMN_NAME |
+-------------+
| field       | 
| field2      | 
| field3      | 
+-------------+
3 rows in set (0.00 sec)

Open in new window

0
 
LVL 17

Expert Comment

by:Aleksandar Bradarić
ID: 24285132
Sorry, forgot the DB name in the FROM clause:
mysql> SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME='myTable';
+-------------+
| COLUMN_NAME |
+-------------+
| field       | 
| field2      | 
| field3      | 
+-------------+
3 rows in set (0.00 sec)

Open in new window

0
 
LVL 17

Expert Comment

by:Aleksandar Bradarić
ID: 24285136
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

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

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
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 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
Course of the Month14 days, 8 hours left to enroll

839 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