why my code not showing the first record?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Fábio Selinger
Forum Newbie
Posts: 7
Joined: Wed Jul 17, 2002 3:21 am
Location: Porto Alegre/RS

why my code not showing the first record?

Post 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;
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)'
Fábio Selinger
Forum Newbie
Posts: 7
Joined: Wed Jul 17, 2002 3:21 am
Location: Porto Alegre/RS

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

:oops: it is
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
Fábio Selinger
Forum Newbie
Posts: 7
Joined: Wed Jul 17, 2002 3:21 am
Location: Porto Alegre/RS

Post by Fábio Selinger »

thnks for all, its works!!!

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