Welcome to WebmasterWorld Guest from 54.196.116.152

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Confusion with mysql data seek

None or Double items displayed

     
2:33 am on Aug 30, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 2, 2004
posts:187
votes: 0


I just keep having trouble with this... Looking for a best approach:

mysql_data_seek($ODisplay, 0);
$row_ODisplay = mysql_fetch_assoc($ODisplay);

do{

Display Stuff here (Hopefully the real stuff) - sometimes I get a blank display sometimes I get a duplicate item
I use: echo $row_ODisplay['FieldName'];
}while($row_ODisplay = mysql_fetch_assoc($ODisplay));

Should I be using foreach{} or something else?
Thanks
M

2:45 am on Aug 30, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Generally I do something like this:

$link = mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("db_name",$link);

$query = "SELECT * FROM table_name";
$result = mysql_query($query,$link) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['col_name1'].'<br />'.$row['col_name2'];//etc...
}
mysql_close($link);

You may also want to look at our library [webmasterworld.com] for some good threads on Basics of extracting data from MySQL using PHP [webmasterworld.com]

Good luck!

2:55 am on Aug 30, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 2, 2004
posts:187
votes: 0


Kool, but what do you do the second time/Third/etc? How do you use:

mysql_data_seek($ODisplay, 0);

I think this is the source of my problem

Thanks
M

4:43 pm on Aug 30, 2006 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


you don't need the mysql_data_seek

what eelixduppy showed will loop through each result using the

while($row = mysql_fetch_array($result)) {

if you follow his lead and get rid of the data_seek you will figure it out

1:07 am on Aug 31, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 2, 2004
posts:187
votes: 0


? " you don't need the mysql_data_seek"?
When I try:

while($row = mysql_fetch_array($result)) {

it skips the first item in the found set. When I use "mysql_data_seek" before the "while" I get all of them, Guess I've solved the problem, I'd just like to understand.

7:20 pm on Aug 31, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


You should get all results using mysql_fetch_array unless you change the internal pointer somewhere. If you want to understand more about these two functions, read up on their documentation and look at their examples.

Go to these links:

Good luck

1:37 pm on Sept 1, 2006 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


if you have it like it is in your first post with
$row_ODisplay = mysql_fetch_assoc($ODisplay);

above the loop then that is why it skipping the first row, you already grabbed it. Remove the extra mysql_fetch above the loop and it should be fine

5:41 pm on Sept 1, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 2, 2004
posts:187
votes: 0


How do I handle using the "mysql_data_seek($ODisplay, 0); " when I do two (or more) of the "while($row = mysql_fetch_array($result)) { ". I still miss the first row sometimes and sometimes I get a duplicate row. Should I do the data_seek before each "while"? Does it mater how I use the $total_Rows result? (I've been using it to skip the "while" with a simple "if($total_Rows <> 0){"

Thanks tons! I really feel like I am in and endless spiral!

M

9:38 pm on Sept 1, 2006 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


yes you could do a data seek before each while, though if you need to go through the reulst more than once you could read them into an array and then walk that

you can use the total_rows to skip the while if there are no results.