Link to home
Start Free TrialLog in
Avatar of designersx
designersx

asked on

please correct the syntax

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

Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

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

Avatar of designersx
designersx

ASKER

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
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 ''
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

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?
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

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?
ur above code will show the record and field name as same , that is not my requirement sir
ASKER CERTIFIED SOLUTION
Avatar of coolersport
coolersport
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if u will see in my code, i am trying to fetch a field name somehow inside the record fetching.
yes it worked, thanks coolersport: sir
No worries. That's why we need the whole context. Not just a snippet of it. Good luck.
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];
        ?>