Page 1 of 1

why my code not showing the first record?

Posted: Thu Aug 22, 2002 12:07 pm
by Fábio Selinger
please, look my code... it´s works... but not showing the first record, is it all right?

Code: Select all

$existe = mysql_query("SELECT * FROM amigos", $conn);
	$rows   = mysql_fetch_array($existe, MYSQL_NUM);
	if($rows){
	
		print("<center>
		<table width='75%' border='1'>");
		
		while (list($nom, $sen, $nic) = mysql_fetch_array($existe, MYSQL_NUM)) &#123;
			print("<tr>
				<td width='50%'>
					$nom
				</td>
				<td width='25%'>
					$sen
				</td>
				<td width='25%'>
					$nic
				</td>
			</tr>");
		&#125;
		print("</table>
		</center>");
	
	&#125;
	else&#123;
		echo "Não há registros!";
		exit;
	&#125;

Posted: Thu Aug 22, 2002 12:44 pm
by nielsene
I'm not an mysql expert, but I would guess that when you do

Code: Select all

$rows= mysql_fetch_array($existe, MYSQL_NUM);
that its advancing the recordset so when you use your while loop its grabbing the second entry and skipping the first.

I think

Code: Select all

$rows = mysql_num_rows($existe);
will fix your problem.

Posted: Thu Aug 22, 2002 1:25 pm
by volka
right.
If you want to test wether the query failed at all or not you check 'if ($existe)'. (if this fails it's a good time to check mysql_error() )
If you want to know if there's a matching record or not you check 'if (mysql_row_num($existe) > 0)'

Posted: Thu Aug 22, 2002 3:17 pm
by Fábio Selinger
Volka, thnks, but i think the name of the function correct is msql_num_rows(), no?

and nielsene, not works! in a good portuguese"DEU NO MESMO!" :(
thnks

Posted: Thu Aug 22, 2002 3:21 pm
by volka
:oops: it is

Posted: Fri Aug 23, 2002 4:35 am
by mikeq
$existe = mysql_query("SELECT * FROM amigos", $conn);
$rows = mysql_fetch_array($existe, MYSQL_NUM);this is getting the first record
if($rows){

print("<center>
<table width='75%' border='1'>");

while (list($nom, $sen, $nic) = mysql_fetch_array($existe, MYSQL_NUM))this part now gets the next and subsequent records {
print("<tr>
<td width='50%'>
$nom
</td>
<td width='25%'>
$sen
</td>
<td width='25%'>
$nic
</td>
</tr>");
}
print("</table>
</center>");

}
else{
echo "Não há registros!";
exit;
}

get rid of the first mysql_fetch_array

Posted: Sat Sep 28, 2002 1:09 pm
by Fábio Selinger
thnks for all, its works!!!

i used mysql_num_rows() and so SHAZAM!!!!
all worked! :D :D :D