klye_g
klye_g

Reputation: 1242

Using variable as column name not working in MYSQL PHP script

I apologize if my question does not make sense. As you can see from the code below, it scans the PHP files, grabs the text between {( )} and stores it into $match.

Now what I am trying to do here is do a query on a table that looks for the column $match where the pageid is equal to the pageid from the URL and where the field(s) are not empty. So far it is only returning Array in the text areas.

Am I setting up the code correctly or am I missing something simple? I know the scanning portion is working because if I echo match I get the text between the {( )} areas. It just isn't doing the query right for some reason. Or like I said I am missing something. Any help would be great. Please let me know if you need further information. (I am not getting any MySQL errors).

$fn = "../templates/".$templateid.".php";

$file = file_get_contents($fn);

preg_match_all("#\{\('(\w+)'\)}#", $file, $matches);   

foreach ($matches[1] as $match) 
    {
    $result = mysql_query("SELECT * FROM pages WHERE $match IS NOT NULL AND linklabel = '$pageID'") or die("Err: ".mysql_error());

    $res = mysql_fetch_array($result);
    $content = $res;
    echo " <div id='tabs-".$match."'>
           <textarea id='".$match."' name='content-".$match."' class='fieldsetstyle'>".$content."</textarea>
                     <script type='text/javascript'>
                     CKEDITOR.replace( '".$match."' );
                     </script>
           </div>";
    }

Upvotes: 0

Views: 1586

Answers (2)

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

Try this

 $result = mysql_query("SELECT * FROM pages WHERE ".$match." IS NOT NULL AND linklabel = '".$pageID."'") or die("Err: ".mysql_error());

Try This Update

Replace this

 foreach ($matches[1] as $match) 

With this

foreach ($matches[1][1] as $match) 

Upvotes: 0

djot
djot

Reputation: 2947

$res IS an array, so try to output the correct column, like

echo $res['linklabel'];

or try var_dump()

var_dump($res);


$content = $res; // Still, $content is an array, not text

Upvotes: 3

Related Questions