0

I found these code from internet and tried to add column with "username" value. But, it can't show the username value from database in the php table.

<?php
  $no = 1;
  $tampil = mysql_query("SELECT * from barangrusak,barang WHERE barang.kode_barang=barangrusak.kode_barang");
  while ($data = mysql_fetch_array($tampil)) {
?>
<tr>
    <td align="center"><?php echo $no; ?></td>
    <td align="center">B00-<?php echo $data['kode_barang']; ?></td>
    <td align="center"><?php echo $data['nama_barang']; ?></td>
    <td align="center"><?php echo $data['jumlah_rusak']; ?></td>
    <td align="center"><?php echo $data['tanggal_rusak']; ?></td>
    <td align="center"><?php echo $data['username']; ?></td>
</tr>
Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
  • 5
    **Please**, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://news.php.net/php.internals/53799), and `mysql_*` functions have been officially removed in PHP 7. Instead you should learn about [prepared statements](https://en.wikipedia.org/wiki/Prepared_statement) and use either `PDO` or `mysqli_*`. If you can't decide, [this article will help to choose your best option](http://php.net/manual/en/mysqlinfo.api.choosing.php). – GrumpyCrouton Jul 30 '19 at 14:55
  • 2
    As said bellow, `mysql_` are depreciated (https://www.php.net/manual/en/function.mysql-query.php) and are you sure you have a `username` column in your table ? `SELECT * ...` is not the best way, better to do `SELECT field1, field2, ...` – Mickaël Leger Jul 30 '19 at 15:01
  • 2
    What does `var_dump($data);` display? (As already mentioned, I suggest you find less outdated learning material: the mysql extension had been deprecated for several years and was removed from PHP in 2015 and [explicit joins](https://stackoverflow.com/questions/44917/explicit-vs-implicit-sql-joins) have been around for decades.) – Álvaro González Jul 30 '19 at 15:16
  • What is in your database? Maybe something's wrong with SQL and you can not reach 'username' field. – zaza Jul 30 '19 at 15:13
  • Note that the `align` attribute is obsolete, too. – Rob Jul 31 '19 at 11:25

1 Answers1

0

first check whether username is in fetch data or not

$data = mysql_fetch_array($tampil);
var_dump($data);

check if username key is exist or not

Rajkumar Sharma
  • 554
  • 1
  • 4
  • 9