tags:

views:

25

answers:

2

I am writing a code where I extract information from tables by entering the query in a php page. So if i write a sql statement in the search box, it would give me the result.

The problem: The mysql statement could be anything that the user enters as explained above. There are a few columns that repeat across tables like that of entered_date and unique_id. I extract the fields using mysql_fetch_assoc. When I do it that way, the duplicate columns get removed. But then I need to check whether there is date field and accordingly change it to a human readable form of "04 January 2010". For this I rely on mysql_field_type. There is the problem.

mysql_field_type it seems takes into account all the fields including the duplicated ones. But my table generated by mysql_fetch_assoc isnt taking the duplicate fields. Therefore as you would understand, from the code below, it is mistakenly thinking that some fields are date fields and I am getting the wrong answer printed.

   while($search_now1=mysql_fetch_assoc($search_now))
            {
        //  echo "in here";
            $checkvalue=0;
            echo "<tr>";
            foreach($search_now1 as $key => $value)
            {
                if($key=='type')
                {
                    switch($value){
                    case 1:$value="Student";break;
                    case 2:$value="Normal";break;
                    case 3:$value="Government";break;
                    case 4:$value="compl";break;
                    case 5:$value="compl-For";break;
                    case 6:$value="Foreign";break;

                    }   

                }
                else
                if($key=='city')
                {
                    $value=city($value);

                }
                else if($key=='state')
                {
                $value=state($value);

                }
                else if($key=='country')
                {
                if($value!='IN')
                        {       
                        $value=state($value);
                        }
                }
                            // this is the error prone area $checkvalue is incremented at the bottom
                else if(mysql_field_type($search_now,$checkvalue)=='date')
                {
                    $value=changedate($value);
                }


                if($value)
                {
                            echo "<td class=\"";
                            if($checkvalue<8)
                            {
                                echo "show";
                            }
                            else
                                echo "none";

                            echo "\"><span class=\"$key\">$value</span></td>";
                }
                else if(!$value)
                            {
                                    echo "<td class=\"";
                            if($checkvalue<8)
                            {
                                echo "show";
                            }
                            else
                                echo "none";

                            echo "\"><span class=\"$key\">-</span></td>";


                }

            $checkvalue++;
            }

How do I correct this?

A: 

It's a bit hard to see what you're doing here (without seeing your database tables or SQL query), but I understand you want to have better control of the types of data returned from a mysql_fetch_assoc(); ie. to be able to distinguish between dates, strings, numbers, and even more comlex types like city-names etc.

Perhaps you could cache the columns in the database locally and perform a dictionary (associative array) lookup.

Adam
When I am using the mysql_fetch_assoc across three tables, then the table that i would be getting is automatically devoid of the duplicate fields... now within this I am trying to find out the field type of each field so that I can send it to the respective function. Mysql_field_type is not removing the duplicates, while mysql_fetch_assoc is..
in continuation - So if I have column names like |UID|NAME|ADDRESS| from table 1 and |UID|ENTERED_DATE|state|country| in another and i take the whole table, then mysql_fetch_assoc sees it as |UID|NAME|ADDRESS|ENTERED_DATE|STATE|COUNTRY| while mysql_field_Type sees it as |UID|NAME|ADDRESS|UID|ENTERED_DATE|STATE|COUNTRY|. Notice the extra UID.. is causing problems. Asa result the ENTERED_DATE isnt going to the changedate function rather the UID is going into the function leading to who lot of problems.
A: 

you should use DESCRIBE tablename mysql query. this will list many properties for all the columns of your table. The type column tells you what type is your column.

Ervin
It will lead tto the same problem again i suppose. The Describe column will display all the column types.. i want to have the duplicate columns removed.