homepage Welcome to WebmasterWorld Guest from 54.234.147.84
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Confusion with mysql data seek
None or Double items displayed
HeadBut




msg:3065229
 2:33 am on Aug 30, 2006 (gmt 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

 

eelixduppy




msg:3065236
 2:45 am on Aug 30, 2006 (gmt 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!

HeadBut




msg:3065241
 2:55 am on Aug 30, 2006 (gmt 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

jatar_k




msg:3066095
 4:43 pm on Aug 30, 2006 (gmt 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

HeadBut




msg:3066726
 1:07 am on Aug 31, 2006 (gmt 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.

eelixduppy




msg:3067823
 7:20 pm on Aug 31, 2006 (gmt 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

jatar_k




msg:3068791
 1:37 pm on Sep 1, 2006 (gmt 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

HeadBut




msg:3069026
 5:41 pm on Sep 1, 2006 (gmt 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

jatar_k




msg:3069341
 9:38 pm on Sep 1, 2006 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved