tags:

views:

62

answers:

2

Part I - Solved.

Hi, I'm trying to print some values on the screen from a table but I having a problem, I don't know much about string, vector and array but I think that my problem is related to them.

I'm getting this on the screen

Fatal error: Cannot use [] for reading ...

My code

 $sql="SELECT * FROM $tbl_name";
 $result=mysql_query($sql) or trigger_error(mysql_error().$sql);
 while($row = mysql_fetch_array($result)){
 $DATA = $row[]; }  //line with probelm
 mysql_close();

 //html part

 <table>
 <? foreach($DATA as $row): ?>
 <tr>
 <td><?=$row['id']?></td>
 //more stuff
 </tr>
 <? endforeach ?>
 </table>

What I'm trying to do is print somevalues form a database. But I'm getting this error.

I'm sorry for any mistake in English, and thanks in advance for any help.

Part II - Edited

Ok, seems like the mysql part is working, i used this before the html. mysql_close(); echo "".$DATA[0][0];

To see if it's working and it printed the right value. But my html part is not printing right.

<html>
<body>
<h1>Lista de usuários</h1>
<table>
<? foreach($DATA as $row): ?>
<tr>
<td><?=$row['id']?></td>
<td><?=$row['nome']?></td>
//more stuff like this
</tr>
<? endforeach ?>
</table>
</body>
</html>

Can you help me? I'm sorry for any mistake in English, and thanks in advance for any help.

+3  A: 

Use:

$DATA = array();
...
while($row = mysql_fetch_array($result)) {
    $DATA[] = $row;
}

The [] operator is used to append a row, which is what we want to do to $DATA. That means that it will never show up on the right side of an assignment statement.

Joseph Mastey
Hi, I got another question and I edited my question could take another look please ?Thanks in advance.
Marcelo
The best would have been to accept this answer and start a new question. I think your problem may lie in the fact there's no ; after the endforeach.
Arkh
A: 

For part 2, as a commenter suggested, remove <? and <?= and use <?php and <?php echo instead. If that doesn't fix the problem, please post the error / unexpected answer you are receiving.

Joseph Mastey
I wasn't getting any error message, just the data was not printed. I was this problem with `<?`, now I changed to `<?php echo` , and it's working.Thanks for the attention.
Marcelo