Page 1 of 2
missed one record from mysql_fetch array...???
Posted: Tue Feb 10, 2004 12:55 am
by apek
i want to display my records from database...
so i use this below code...the one i use often...
Code: Select all
<?php
$bil=1;
while($senarai=mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<TR>
<TD><font face="verdana" size="2"><? echo $bil; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["ID"]; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["alat"]; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["jumlah"]; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["kategori"]; ?></font></TD>
</TR>
<?php
$bil++;
}
?>
but this time there one record missing...
the rest is displayed correctly...
but only one is not...
it seems like its not fetched at all!!!
why???
when i checked mysql,the missing record is on the top...the first record...
so any ideas???

Posted: Tue Feb 10, 2004 1:21 am
by mahara
Try to debug first the returned resultset using 'print_r()' or 'var_dump()' function. You can check if all all rows you expect are returned in the resultset.
Hope helps.
Posted: Tue Feb 10, 2004 1:25 am
by apek
mahara..how to do that...??
i'm a newbie actually....
u're indonesian...
i know how to speak yours...
BTW,heres my full code..maybe it can help u to diagnose it...
Code: Select all
<?php
include("../inc/config.php");
include("../inc/dateandtime.php");
function query($query, $link, $line, $file)
{
$result = mysql_query($query);
if (empty($result) && ini_get('display_errors') == 1)
{
echo mysql_error().": ";
echo mysql_errno()."<br />\n";
echo $query."<br />\n";
echo "@ $line in $file\n";
}
return $result;
}
?>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Zend Studio">
</HEAD>
<BODY>
<form name="myform" action="paparKomponen.php">
<table width="100%" border="1" bgcolor="#666666" bordercolor="#333333" align="center">
<tr>
<td align="center">
<font face="verdana" size="2" color="white"><b>
MODUL PAPARAN MAKLUMAT KOMPONEN PAM CENTRIFUGEL
</b></font>
</td>
</tr>
</table>
<br>
<?php
$kategori=$_POST["kategori"];
$alat=$_POST["komponen"];
echo $alat;
//***********PANGGIL DATA DARIPADA PANGKALAN DATA******
if ($_POST["type"]!="")
{
//*******************PANGGIL SEMUA**************
if ($_POST["type"]=="A")
{
$result=query("Select * from komponen", $link, __LINE__, __FILE__);
$senarai=mysql_fetch_array($result, MYSQL_ASSOC);
}
else
{
if ($_POST["type"]=="BJ")
{
$result=query("Select * from komponen where kategori = '$kategori'", $link, __LINE__, __FILE__);
$senarai=mysql_fetch_array($result, MYSQL_ASSOC);
}
else
{
if ($_POST["type"]=="BK")
{
$result=query("SELECT * FROM komponen where alat = '$alat'", $link, __LINE__, __FILE__);
$senarai=mysql_fetch_array($result, MYSQL_ASSOC);
}
}
}
}
?>
<?php
if (empty($result))
{
?>
<br>
<table width="100%" border="0"align="center">
<tr>
<td align="center">
<font face="verdana" size="2" color="red"><b>
Maaf ! Tiada maklumat di dalam pangkalan data
</b></font>
</td>
</tr>
</table>
<P><BR>
<CENTER>
<input style="BACKGROUND-COLOR: #cccccc; BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; COLOR: #333333; FONT-FAMILY: Verdana; FONT-SIZE: 7pt" type="submit" value="BACK" name="back">
</CENTER>
<?php
}
else
{
?>
<!--**********TABLE PAPARAN VIEW********************-->
<TABLE BORDER="1" WIDTH="100%" BGCOLOR="#eeeeee" align=center cellPadding=0 cellSpacing=0 height=1 style="HEIGHT: 1px">
<TR ALIGN="CENTER">
<TD ALIGN="CENTER" BGCOLOR="#666666">
<font face="verdana" size="2" color="white"><strong> Bil </strong></font>
</TD>
<TD ALIGN="CENTER" BGCOLOR="#666666">
<font face="verdana" size="2" color="white"><strong> ID </strong></font>
</TD>
<TD ALIGN="CENTER" BGCOLOR="#666666">
<font face="verdana" size="2" color="white"><strong> Alat </strong></font>
</TD>
<TD ALIGN="CENTER" BGCOLOR="#666666">
<font face="verdana" size="2" color="white"><strong> Jumlah </strong></font>
</TD>
<TD ALIGN="CENTER" BGCOLOR="#666666">
<font face="verdana" size="2" color="white"><strong> Kategori </strong></font>
</TD>
</TR>
<?php
$bil=1;
while($senarai=mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<TR>
<TD><font face="verdana" size="2"><? echo $bil; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["ID"]; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["alat"]; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["jumlah"]; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["kategori"]; ?></font></TD>
</TR>
<?php
$bil++;
}
?>
</table>
<P><BR>
<CENTER>
<input style="BACKGROUND-COLOR: #cccccc; BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; COLOR: #333333; FONT-FAMILY: Verdana; FONT-SIZE: 7pt" type="submit" value="BACK" name="back">
</CENTER>
<?php
}
?>
</form>
<pre><?php
print_r($_POST); // print_r()
?></pre>
</BODY>
</HTML>
Posted: Tue Feb 10, 2004 2:30 am
by mahara
Yes, I'm an Indonesian. So are you; I know it from your code

. But unfortunately, this forum is English-based.
According to your code below,
Code: Select all
<?php
$bil=1;
while($senarai=mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<TR>
<TD><font face="verdana" size="2"><? echo $bil; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["ID"]; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["alat"]; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["jumlah"]; ?></font></TD>
<TD><font face="verdana" size="2"><? echo $senarai["kategori"]; ?></font></TD>
</TR>
<?php
$bil++;
}
?>
you can change it for a while to
Code: Select all
<?php
$senarai=mysql_fetch_array($result, MYSQL_ASSOC);
print_r($senarai);
?>
See whether it returns all rows you want.
BTW this is a tip from me, instead you use
Code: Select all
if ( expression )
{
your code ....
}
else
{
if ( expression )
{
your code ...
}
}
your better use this
Code: Select all
if ( expression )
{
your code ....
}
elseif ( expression )
{
your code ...
}
Both logically is right, but the first may not syntactically right and can confuse in a complex situation. However it's just a tip from me. Let you conform first to the style or it can break your code.
Hope helps.
Posted: Tue Feb 10, 2004 3:58 am
by apek
actually i'm a malaysian...
i try first your tips....
thanx...
Posted: Tue Feb 10, 2004 4:08 am
by malcolmboston
have a look at this
here
is that your problem?
i gave quite a detailed description if it is
good luck
Posted: Tue Feb 10, 2004 4:12 am
by apek

....
i tried your tips....
but it still the same....
Posted: Tue Feb 10, 2004 4:13 am
by mahara
Well, I think I've miss-guessed. Sorry ...
Posted: Tue Feb 10, 2004 4:35 am
by mahara
Yes, malcolmboston is right. You have get your first resultset through this code below
Code: Select all
$result=query("Select * from komponen", $link, __LINE__, __FILE__);
$senarai=mysql_fetch_array($result, MYSQL_ASSOC);
It's not clear the reason why you fetch row from the result.
Posted: Tue Feb 10, 2004 4:36 am
by malcolmboston
i thought it was.....
Posted: Tue Feb 10, 2004 5:03 am
by apek
it works!!!!
but....
another problem raise....
if u look at my code, i'm trying to make query based on user selection...
if the user select radio button ALL, then it'll do this:
Code: Select all
if ($_POST["type"]="ALL")
{
$result=query("Select * from komponen", $link, __LINE__, __FILE__);
//$senarai=mysql_fetch_array($result, MYSQL_ASSOC);
}
and so on like this:
Code: Select all
elseif ($_POST["type"]="KATEGORI")
{
$result=query("Select * from komponen where kategori = '$kategori'", $link, __LINE__, __FILE__);
//$senarai=mysql_fetch_array($result, MYSQL_ASSOC);
}
elseif ($_POST["type"]="KOMPONEN")
{
$result=query("SELECT * FROM komponen where alat = '$alat'", $link, __LINE__, __FILE__);
//$senarai=mysql_fetch_array($result, MYSQL_ASSOC);
}
the new problem is i cant display record based on the query
Code: Select all
$result=query("Select * from komponen where kategori = '$kategori'", $link, __LINE__, __FILE__);
and this query
Code: Select all
$result=query("SELECT * FROM komponen where alat = '$alat'", $link, __LINE__, __FILE__);
i only can display all records only.....
why???
Posted: Tue Feb 10, 2004 5:12 am
by malcolmboston
i dont know what this means........
so i cant help you
remember
googleis your friend!
Posted: Tue Feb 10, 2004 5:15 am
by apek
oooo...
thats my custom query function..
here it is:
Code: Select all
function query($query, $link, $line, $file)
{
$result = mysql_query($query);
if (empty($result) && ini_get('display_errors') == 1)
{
echo mysql_error().": ";
echo mysql_errno()."<br />\n";
echo $query."<br />\n";
echo "@ $line in $file\n";
}
return $result;
}
anyone to help me???

Posted: Tue Feb 10, 2004 5:30 am
by mahara
Does your following code return any value?
Code: Select all
$kategori=$_POST["kategori"];
$alat=$_POST["komponen"];
Try to use 'print_r()' function first, if it still returns no value try to use 'var_dump()'. If no value returned, you might have not pass the variables properly.
Hope helps.
Posted: Tue Feb 10, 2004 5:32 am
by apek
yes..they do...
i already tried to echo them....
but still no solution...
