please correct the syntax

designersx
designersx used Ask the Experts™
on
please correct the syntax

$field is dynamic, there is a problem of quotes.
<?php while($rec=mysql_fetch_array($qid)){
		echo $rec['$field'];
	?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try,
echo $rec['field'];

Open in new window

$field is not correct,   "field" might be database field where you are trying from the result
If the $field is dynamic and you have defined and set value for it in advance, then the code should be like below:
<?php while($rec=mysql_fetch_array($qid)){
   echo $rec[$field];
?>

Open in new window

Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Author

Commented:
logudotcom: sir this is where the trick is
$field is not static. it contains the field names which are dynamic, can be changed any time.
so we can't write as you said.

coolersport: as u said to write me , error is Cannot use object of type stdClass as array

Author

Commented:
sir actually i have dynamic field names and i am displaying the dynamic field names , that is ok and now i want to display the record of those fields. that's why i wrote

while($rec=mysql_fetch_array($qid)){
 $rec[''] // i know that here we write the field name whose record we want to display but suppose if someone changes the field name then there would be a problem , that's why i asked you to tell me the syntax
}

i m increasing the marks of this question.
not sure

try likw this

echo $rec[ +"'" + $field+ "'"];

concatinate ''
not sure

try likw this

echo $rec[ ."'" + $field . "'"];

concatinate ''

Author

Commented:
hope u got me once again, i need to write

$rec[  // write dynamic field name here and not static ];
Try this:
<?php while($rec=mysql_fetch_array($qid))
   foreach($rec as $fieldname => $fieldvalue)
       echo $fieldvalue; // or echo $fieldname, it is up to you at this point
?>

Open in new window

Author

Commented:
with first code  

Cannot use object of type stdClass as array

with ur second code

Parse error: syntax error, unexpected '.', expecting ']'
LOL, can you post the whole php code of this file?

Author

Commented:
code
<form action="javascript:save()" method="post" name="frm">
<table border="1" bgcolor="#CCCCCC" width="80%"><tr><td colspan="2" align="center">Form</td></tr>
<tr><?php
$sql="select * from `$tname`";
$qid=mysql_query($sql) or die("could not select".mysql_error());
$i = 1;
while($rec=mysql_fetch_array($qid)){
	while ($i < mysql_num_fields($qid)) {
    	$rec = mysql_fetch_field($qid, $i);
		$field = $rec->name;
		?><td><?php echo $field; ?></td>
		<td><?php echo $rec[ +"'" + $field+ "'"]; ?></td>
    	<?php $i++; } } ?>
</tr></table></form>

Open in new window

Author

Commented:
actually the concept is i want to fetch the field name ans then fetch the record of that field. all are dynamic , that's why?

Author

Commented:
ur above code will show the record and field name as same , that is not my requirement sir
Haha, you overwrite $rec.
<form action="javascript:save()" method="post" name="frm">
<table border="1" bgcolor="#CCCCCC" width="80%"><tr><td colspan="2" align="center">Form</td></tr>
<tr><?php
$sql="select * from `$tname`";
$qid=mysql_query($sql) or die("could not select".mysql_error());
$i = 1;
while($rec=mysql_fetch_array($qid)){
        while ($i < mysql_num_fields($qid)) {
        $meta = mysql_fetch_field($qid, $i);
                $field = $meta->name;
                ?><td><?php echo $field; ?></td>
                <td><?php echo $rec[$field]; ?></td>
        <?php $i++; } } ?>
</tr></table></form>

Open in new window

Author

Commented:
if u will see in my code, i am trying to fetch a field name somehow inside the record fetching.

Author

Commented:
yes it worked, thanks coolersport: sir
No worries. That's why we need the whole context. Not just a snippet of it. Good luck.

Commented:
If I am not out of point, php parser does not get inside a string surrounded by single quotes, thus:
<?php while($rec=mysql_fetch_array($qid)){
                echo $rec[$field];
        ?>

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial