开发者

php mysql error beginner

开发者 https://www.devze.com 2023-01-02 22:45 出处:网络
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 th

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&g开发者_如何转开发t;<?=$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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号