Citra45Abadi Citra45Abadi - 1 month ago 6
MySQL Question

mysqli query select * not returning a certain field

only 1 field is not returned, the rest of it is returned correctly

the problem is I used the same query for other page and it returned everything okay

the problematic PHP :

$sql=mysqli_query($con,"SELECT * FROM produk INNER JOIN kategori ON produk.id_kategori=kategori.id_kategori WHERE id_produk='$_GET[id]'");
$r=mysqli_fetch_array($sql);

echo"judul produk : $r[nama_produk] <br />
nama kategori : $r[nama_kategori]<br />
judul gambar : $r[gambar]";


the results :

judul produk : test mesin kyocera

nama kategori : kyocera

judul gambar :

another php, used on other page which is pretty much the same but works fine :

$sql=mysqli_query($con,"SELECT * FROM produk INNER JOIN kategori2 ON produk.id_kategori2=kategori2.id_kategori2 ORDER BY rand() DESC LIMIT 25");
while($r=mysqli_fetch_array($sql)){
include "diskon_stok.php";
echo"
<div class='container_produk'>
<div class='preview'>
<span style='font-size : 22px'>$r[nama_kategori2]</span>
<span style='font-size : 14px'>$r[preview]</span>
<button class='pre-button'><a href='produk-$r[id_produk]-$r[nama_produk].html'>Detail</a></button>
</div>
<img style='margin-left : 5%;' src='foto_produk/small_$r[gambar]'/>
<div class='detail'>
<span style='font-size : 22px'>$r[nama_produk]</span>
$divharga
<br />
<br />
</div>

</div>
";
}


now the field not returned is $r[gambar] on Code 1, but on Code 2 $r[gambar] is returned and thus the image is showing on browser

so did I do something wrong somewhere?

Answer

turns out my query is wrong

it should be written like this

$sql=mysqli_query($con,"SELECT * FROM produk,kategori WHERE produk.id_kategori=kategori.id_kategori AND id_produk='$_GET[id]'");
$r=mysqli_fetch_array($sql);

no need to type INNER JOIN or JOIN