Retrieve multiple rows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Retrieve multiple rows

Post by Kingo »

Hello,
I'm trying to retrieve multiple rows{ My table has 24 rows}. But my code below displays only the first row. Your help is really appreciated.
Here is my code.

Code: Select all

$result1 = MYSQL_QUERY("SELECT * from data1 WHERE UserName='$username'")
   or die ("Name and password not found or not matched");

$worked1 = mysql_fetch_array($result1);

if($worked1)
{
$Y_Name1 =  $worked1["Year"]; 
$T_Name1 =  $worked1["Type"]; 
$S_Name1 =  $worked1["Subject"];
$V_Name1 =  $worked1["Value"];

echo "Year : $Y_Name1 <br>";
echo "Type : $T_Name1<br>";
echo "Subject : $S_Name1<br>";
echo "Value : $V_Name1<br>";

}
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Re: Retrieve multiple rows

Post by qads »

Code: Select all

$result1 = MYSQL_QUERY("SELECT * from data1 WHERE UserName='$username'")or die ("Name and password not found or not matched");
while($worked1 = mysql_fetch_array($result1))
{
$Y_Name1 =  $worked1["Year"]; 
$T_Name1 =  $worked1["Type"]; 
$S_Name1 =  $worked1["Subject"];
$V_Name1 =  $worked1["Value"];
echo "Year : $Y_Name1 <br>";
echo "Type : $T_Name1<br>";
echo "Subject : $S_Name1<br>";
echo "Value : $V_Name1<br>";
}
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

Thanx very much. I have another question. I want to display the values in a form. I mean i want to bind the form to the database and siaply the values in the form. Your help is appreciated. [ I was able to do for single row--but here there are multiple rows and i dont know how to handle them]
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

i think this is what you want...

Code: Select all

<textarea name="NAME" cols="80" rows="6">$WHATEVERVALUE YOU WANT IN HERE(MULTI LINE)</textarea>

//or

<input type="text" name="NAME" value="$WHATEVERVALUE YOU WANT IN HERE(SINGLE LINE)">
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

Nope.

Code: Select all

$result1 = MYSQL_QUERY("SELECT * from data1 WHERE UserName='$username' order by 'Year' ")   or die ("Name and password not found or not matched");
echo "<table border="1" >";

while($worked1 = mysql_fetch_array($result1))
{
$Y_Name1 =  $worked1["Year"]; 
$T_Name1 =  $worked1["Type"]; 
$S_Name1 =  $worked1["Subject"];
$V_Name1 =  $worked1["Value"];

echo "<tr><td>$Y_Name1 </td>";
echo "<td>$T_Name1</td>";
echo "<td>$S_Name1</td>";
echo "<td>$V_Name1</td></tr>";

}
echo "</table>";
With the above code I'm displaying the contents of the table from the datadase onto a HTML table. I want to display the contents of the table from the database onto a HTML FORM. I mean how do i assign values to the text boxes in the form. I'm confused because I'm having multiple rows.

If it were single row i would do thsi way and it works..Please suggest me what to do for multiple rows.

Code: Select all

$result = MYSQL_QUERY("SELECT * from data WHERE UserName='$username'and PassWord='$password'")
   or die ("Name and password not found or not matched");

$worked = mysql_fetch_array($result);

if($worked)
{
$S_Name =  $worked["School_Name"]; 
$P_Name =  $worked["Principal_Name"]; 

echo "<form name="form9" method="post" action="review.php"> ";
echo "<div align="center"> <table width="640" border="0" align="center" cellpadding="2" cellspacing="0"> ";
echo "<tr><td colspan="7" align="center"> ";

echo "School Name: <input name="schoolname" type="text"  value= "$S_Name"  size="35"> ";
echo "</tr> <tr> <td width="118" valign="top">Principal:</td> ";

echo "<td width="122"><input name="principal" type="text"  value= "$P_Name"  size="15"></td>";

echo "<div align="center"><br><input type="submit" name="Submit" value="Submit"> ";


echo "</div></form>";
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

so close

Post by phpScott »

you are so close by the looks of it.

take your while loop and put it after your if statement, essential combine the two using your while loop to keep assembling each row of your form.

phpscott
Kingo
Forum Contributor
Posts: 146
Joined: Thu Jun 03, 2004 9:38 am

Post by Kingo »

I am not able to get the functionality. If any one could give me a idea, i would really appreciate it
Thanx
Post Reply