Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_fetch_row question??

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
I used the following code to display the name, and photo records from employee table


$query = mysql_query("SELECT * FROM emp");


while ($row = mysql_fetch_row($query)) {
echo &quot;<table>&quot;;

echo &quot;<tr><td><img src=\&quot;$row[3]\&quot; border=\&quot;0\&quot;></td></tr>&quot; . &quot;<tr><td><a href=\&quot;detailes.php?hid=$row[0]\&quot;>$row[1]</a></td></tr></table>&quot;;

}
?>

this display the data in the same place, Now who can I display i.e. first record of the data in a cell at the top of the page, and the forth record in some other cell in the page. When I used: if ($row = mysql_fetch_row($query)) this return the first record only and when I use: while ($row = mysql_fetch_row($query)) this retrive all the data in the same place of the page.

Thanks in advance.
 
your question is a little vague since in both places you refer to the mysql_fetch_row($query) function...I will assume that as you referenced the need to show all data you want to use mysql_fetch_array($query)

<?

echo &quot;<table>&quot;;
while ($row = mysql_fetch_array($query)) {

echo &quot;<tr><td><img src=\&quot;$row[3]\&quot; border=\&quot;0\&quot;></td></tr>&quot; . &quot;<tr><td><a href=\&quot;detailes.php?hid=$row[0]\&quot;>$row[1]</a></td></tr>&quot;;


}
echo &quot;</table>&quot;;
?>

hth Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
OK, I will repeat the question
In the employee table I have the following data:
Employee1, image1
Employee2, image2
Employee3, image3
Employee4, image4
I want to display Employee1, image1 in the some place in the page, and Employee3, image3 in other place in the same page.
 
If you have no way of selecting only the records for Employee1 and Employee3, then you have two choices.

One is to perform an extra mysql_fetch_row() without doing anything with the data. After you get the row for Employee1 and output it, then issue another mysql_fetch_row() but do nothing with the data. Then you can perform mysql_fetch_row() again to get the data for Employee3. This will work if you need the data for Employee1 and Employee3, but is terribly inefficient if you need the data for Employee1 and Employee33. For that, use mysql_data_seek().

mysql_data_seek() ( allows you to retrieve the rows of a result set in any arbitrary order. Issue mysql_data_seek() to set the row of data you want [one gotcha: rows begin with number 0, not 1], then issue a mysql_fetch_row() to retrieve the data in that row. ______________________________________________________________________
TANSTAAFL!
 
I would first fetch all data at the top of the page in an array...
<?php
$i=0;
$data = array();

$query = &quot;SELECT * FROM emp&quot;;
$result = mysql_query($query);
$data[$i] = mysql_fetch_array($result, MYSQL_ASSOC);
while($data[$i]){
$i++;
$data[$i] = mysql_fetch_array($result, MYSQL_ASSOC);
}
$len = sizeof($data);//if needed

/* then access you data anywhere in the page like... */

echo $data[0]['Employee'];
echo $data[0]['Image'];

/*somewhere else in the page */
echo $data[3]['Employee'];
echo $data[3]['Image'];

?>
 
Binaryshi,

And if the table has a million records in it?

Sucking down the table is not a good general strategy. Your script can eat up an enourmous amount of resources trying to read that many records into an array. ______________________________________________________________________
TANSTAAFL!
 
without submitting a list of which employees you would want to see, it gets nasty as you can see...what about showing some form where the user can submit a list of people who he/she would like to see (via radio buttons or checkboxes where an idea can be passed to the server) to retrieve a small finite amount of data and make the thing simpler

the basic idea is that you need to identify the data you want to see and then present. data at random is tricky at best and nightmare at worst Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top