3

I am trying to compare the field names in the table against the $_GET field names and if it exists in the table create a Query string, and i am trouble loading the mysql_field_name into an array if i do them individually like $t1 = mysql_field_name($result,1); it works but loading them all like $vars = mysql_field_name($result); dose not seem to work.

This dose not work

$query = array();
$result = mysql_query("SELECT * FROM search_prof");
$vars = mysql_field_name($result);

foreach ($vars as $v)
{
    if (isset($_GET[$v]))
    {
        $query[] = $v.' = "'.addslashes($_GET[$v]).'"';
    }
}
$query = implode(' AND ', $query); 

This works

$t1 = mysql_field_name($result,1);
$t2 = mysql_field_name($result,2);
$t3 = mysql_field_name($result,3);
$t4 = mysql_field_name($result,4);
$t5 = mysql_field_name($result,5);


    $query = array();
    $result = mysql_query("SELECT * FROM search_prof");
    $vars = array('$t1', '$t2', '$t3', '$t4', '$t5'); 

    foreach ($vars as $v)
    {
        if (isset($_GET[$v]))
        {
            $query[] = $v.' = "'.addslashes($_GET[$v]).'"';
        }
    }
    $query = implode(' AND ', $query); 

3 Answers 3

2

You should have a look at the php documentation: http://php.net/manual/de/function.mysql-field-name.php

The second parameter in this function is NOT optional, so you have to take your second approach.

If you want make it dynamic anyway, you can use the mysql_num_field function, which gives you the number of columns. Afterwards you can build a loop, which iterates this much times, calling everytime the mysql_field_name function.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to make your current approach work, I guess you could do something like:

//Get fields in table
$numberOfFields = mysql_num_fields($result) - 1;

$fields = array();
for ($i = 0; $i <= $numberOfFields; $i++) {
    $fields[] = mysql_field_name($result, $i);
}

//Intersect
$parameters = array_intersect(array_keys($_GET), $fields);

//Query
$q = array();
foreach($parameters as $parameter) {
    $q[] = $v . '="' . addslashes($_GET[$parameter]) . '"';
}

$q = implode(" AND ", $q);

You could probably also try running a DESCRIBE-query on the table instead and parsing those results, but don't know about the performance of DESCRIBE.

Comments

1

You could use something like this to get the fields.

$query = array();
$result = mysql_query("SELECT * FROM search_prof");
$numFields = mysql_num_fields($result);
$vars = array();
for ( $i = 0; $i < $numFields; $i++ )
    $vars[] = mysql_field_name($result, $i);

foreach ($vars as $v)
{
    if (isset($_GET[$v]))
    {
        $query[] = $v.' = "'.addslashes($_GET[$v]).'"';
    }
}
$query = implode(' AND ', $query); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.