Page 1 of 1

problems with records

Posted: Mon Feb 02, 2004 9:53 am
by dizeta
hello to everyboby,
i'm starting to learn php ( :mrgreen: ), but some problems.....
i've this script that once log in, shows to an user some privates files.

the problem is:
for example, i've three record stored into a table call "user"

record 1
record 2
record 3


but this script prints on screen only the last two

record 2
record 3


i really don't understand why???

ok, the script:

Code: Select all

<? 
include("dbconnect.php");
include("session.php");
?> 

<html> 
<head> 
<title>Area riservata</title> 
</head> 
<body> 




<br><br><center><font face="verdana" size="2">
<?
$result=mysql_query("SELECT files.filename,files.filesize, files.id_files FROM files, utenti WHERE utenti.id_utente = files.id_user AND utenti.id_utente='$verified_user'")or die (mysql_error()); 


if(mysql_num_rows($result)==0) &#123; 
  $filename=("-"); $filesize=("-"); $idutente=("-");
&#125; 
else &#123; list($filename,$filesize,$idutente)=mysql_fetch_row($result); &#125;
  

$number_cols = mysql_num_fields($result); 

$work=mysql_query("SELECT nome FROM utenti WHERE id_utente='$verified_user'"); 
list ($nome)=mysql_fetch_row($work);


echo "Benvenuto <b>$nome</b>, questi sono i tuoi files:\n"; 
echo "<table border="1" style="border-collapse: collapse" bordercolor="#cccccc"> \n"; 
echo "<tr align="center">\n"; 
for ($i=0; $i<$number_cols; $i++) 
&#123; 
echo "<th>" . mysql_field_name($result, $i). "</th>\n"; 
&#125; 
echo "</th>\n"; 

while ($row = mysql_fetch_row($result)) 
&#123; 
echo "<tr align="left">\n"; 
for ($i=0; $i<$number_cols; $i++) 
&#123; 
echo "<td>"; 
if(!isset($row&#1111;$i])) 
&#123; echo "NULL";&#125; 
else 
&#123; echo $row&#1111;$i];&#125; 
echo "</td>\n"; 
&#125; 
echo "</tr>\n"; 
&#125; 
echo "</table>"; 

?> </center>
</font>
</body> 
</html>
thanks for the patience,

ciao

Posted: Mon Feb 02, 2004 10:49 am
by malcolmboston
just a guess as i havent properly examined your code, however i had teh same problem a while ago and fixed it relatively easily

if your running a loop statement, you must get the first result first otherwise it will skip it out completely, i believe this is the problem you are having.

hope this helps you out

Posted: Mon Feb 02, 2004 11:10 am
by dizeta
hi, thanks for your replay.

could you show me an example? possibly related to my script?
i think that i've problems to understand the "base concept".

for sure, an example will be more clear for me.

thanks again!
:)

Posted: Mon Feb 02, 2004 11:14 am
by malcolmboston
2 mins ill go and find my script :lol:

Posted: Mon Feb 02, 2004 11:25 am
by malcolmboston
heres my full script, i use it to get all information for registered members for a profiling system

Code: Select all

<?php
// database variables for connections
$host = "localhost";
$user = "malcolmboston";
$password = "xxxxxxx";
$DBname = "xxxxxxx";
$tablename = "login";
//connection variables completed
// establishing connections
$link = mysql_connect ($host, $user, $password);
//connection established
//the query defined
$query = "SELECT * FROM login WHERE show_profile = 'yes' AND access_level = 'Admin' ORDER BY 'access_level' ASC";
// select the database
mysql_select_db($DBname);
// query the database
$result = mysql_query($query);
// display all the results for all the admin
$data = mysql_fetch_array($result, MYSQL_ASSOC);
print "
<form name=$data[username] method=post action=profile_details.php target=_blank><tr bgcolor=#46B5FF>
    <td width=30%><strong>&nbsp;$data[username]</strong></td>
    <td width=20%><a class=main-nav href=mailto:$data[email]>E-Mail Me</a></td>
    <td width=20%><font face=arial>$data[access_level]</font></td>
    <td width=20%><input class=push name=$data[username] type=submit id=$data[username] value=View Profile>
	<input name=username2pass type=hidden id=username2pass value=$data[username]>
	</td>
  </tr></form>";
while ($row = mysql_fetch_assoc($result))
print "
<form name=$row[username] method=post action=profile_details.php target=_blank>
<tr bgcolor=#46B5FF>
    <td><strong>&nbsp;$row[username]</strong></td>
    <td><a class=main-nav href=mailto:$row[email]>E-Mail Me</a></td>
    <td><font face=arial>$row[access_level]</font></td>
    <td><input class=push name=$row[username] type=submit id=$row[username] value=View Profile>
	<input name=username2pass type=hidden id=username2pass value=$row[username]></td>
  </tr><form>";
//fetch all the results for all the members
$query2 = "SELECT * FROM login WHERE show_profile = 'yes' AND access_level = 'Member' ORDER BY 'access_level' ASC";
// select the database
mysql_select_db($DBname);
// query the database
$result2 = mysql_query($query2);
// display all the results for all the admin
$data2 = mysql_fetch_array($result2, MYSQL_ASSOC);
print "
<form name=$data2[username] method=post action=profile_details.php target=_blank>
<tr bgcolor=#D2EDFF>
    <td width=30%><strong>&nbsp;$data2[username]</strong></td>
    <td width=20%><a class=main-nav href=mailto:$data2[email]>E-Mail Me</a></td>
    <td width=20%>$data2[access_level]</td>
    <td width=20%><input class=push name=$data2[username] type=submit id=$data2[username] value=View Profile>
	<input name=username2pass type=hidden id=username2pass value=$data2[username]></td>
  </tr></form>";
now lets break down my script
you will notice that on my script that i make sure it inserts my first result and then the loop is created thereafter
for eg in my administrators

Code: Select all

$link = mysql_connect ($host, $user, $password);
//connection established
//the query defined
$query = "SELECT * FROM login WHERE show_profile = 'yes' AND access_level = 'Admin' ORDER BY 'access_level' ASC";
// select the database
mysql_select_db($DBname);
// query the database
$result = mysql_query($query);
// display all the results for all the admin
$data = mysql_fetch_array($result, MYSQL_ASSOC);
print "
<form name=$data[username] method=post action=profile_details.php target=_blank><tr bgcolor=#46B5FF>
    <td width=30%><strong>&nbsp;$data[username]</strong></td>
    <td width=20%><a class=main-nav href=mailto:$data[email]>E-Mail Me</a></td>
    <td width=20%><font face=arial>$data[access_level]</font></td>
    <td width=20%><input class=push name=$data[username] type=submit id=$data[username] value=View Profile>
	<input name=username2pass type=hidden id=username2pass value=$data[username]>
	</td>
  </tr></form>";
while ($row = mysql_fetch_assoc($result))
print "
<form name=$row[username] method=post action=profile_details.php target=_blank>
<tr bgcolor=#46B5FF>
    <td><strong>&nbsp;$row[username]</strong></td>
    <td><a class=main-nav href=mailto:$row[email]>E-Mail Me</a></td>
    <td><font face=arial>$row[access_level]</font></td>
    <td><input class=push name=$row[username] type=submit id=$row[username] value=View Profile>
	<input name=username2pass type=hidden id=username2pass value=$row[username]></td>
  </tr><form>";
the first record is created here

Code: Select all

$link = mysql_connect ($host, $user, $password);
//connection established
//the query defined
$query = "SELECT * FROM login WHERE show_profile = 'yes' AND access_level = 'Admin' ORDER BY 'access_level' ASC";
// select the database
mysql_select_db($DBname);
// query the database
$result = mysql_query($query);
// display all the results for all the admin
$data = mysql_fetch_array($result, MYSQL_ASSOC);
print "
<form name=$data[username] method=post action=profile_details.php target=_blank><tr bgcolor=#46B5FF>
    <td width=30%><strong>&nbsp;$data[username]</strong></td>
    <td width=20%><a class=main-nav href=mailto:$data[email]>E-Mail Me</a></td>
    <td width=20%><font face=arial>$data[access_level]</font></td>
    <td width=20%><input class=push name=$data[username] type=submit id=$data[username] value=View Profile>
	<input name=username2pass type=hidden id=username2pass value=$data[username]>
	</td>
  </tr></form>";
and then looped here straight after

Code: Select all

while ($row = mysql_fetch_assoc($result))
print "
<form name=$row[username] method=post action=profile_details.php target=_blank>
<tr bgcolor=#46B5FF>
    <td><strong>&nbsp;$row[username]</strong></td>
    <td><a class=main-nav href=mailto:$row[email]>E-Mail Me</a></td>
    <td><font face=arial>$row[access_level]</font></td>
    <td><input class=push name=$row[username] type=submit id=$row[username] value=View Profile>
	<input name=username2pass type=hidden id=username2pass value=$row[username]></td>
  </tr><form>";
hope this helps you out, i know this works because ive just taken it from my site