Php mysql error beginner

Part I - Solved.

Hi, I am trying to print some values โ€‹โ€‹on the screen from a table, but I have a problem, I know little about the string, vector and array, but I think my problem is related to them.

I get it on 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 some values โ€‹โ€‹from a database. But I get this error.

I apologize for any mistake in the English language and in advance for any help.

Part II - Edited

Well, it looks like the mysql part is working, I used this before html.     mysql_close();   echo "".$DATA[0][0];

To find out if it works, and printed the correct value. But my html part is not printing correctly.

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

Could you help me? I apologize for any mistake in English, and in advance for any help.

+3
2

:

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

[] , $DATA. , .

+3

2, , <? <?= <?php <?php echo. , / .

0

Source: https://habr.com/ru/post/1749578/


All Articles