Welcome to WebmasterWorld Guest from 54.147.20.131

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Confusion with mysql data seek

None or Double items displayed

     

HeadBut

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

10+ Year Member



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

eelixduppy

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

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



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!

HeadBut

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

10+ Year Member



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

jatar_k

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

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



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

HeadBut

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

10+ Year Member



? " 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.

eelixduppy

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

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



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

jatar_k

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

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



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

HeadBut

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

10+ Year Member



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

jatar_k

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

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



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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month